jaime.virruetaf@gmail.com's avatar

How add additional route to the method resource

Dear as I can do to add an additional method by default when creating the routes using the 'resource' method and the classic CRUD methods the additional is 'active' which is a flag and I use them in almost all cruds, I have got it modifying the files that are in vendor and works great the problem is when you do an update .. it will be deleted ...

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

It is never a good idea to update vendor packages. If this is needed for a small number of routes, then simply add the custom route before the resource route(s), e.g.

Route::get('things/active', 'ThingsController@active');
Route::resource('things', 'ThingsController');

If you need it for lots of route definitions, then consider registering a Route macro:

Route::macro('resourceAndActive', function ($uri, $controller) {
    Route::get("{$uri}/active", "{$controller}@active")->name("{$uri}.active");
    Route::resource($uri, $controller);
});

You would use this similarly to the normal resource route declaration:

Route::resourceAndActive('users', 'UserController');

And it will register the usual resource routes along with an active route. It would probably need to be tweaked to allow for something other then a GET route, and to receive the usual options that the resource route accepts, but this should give you an idea to get started with.

1 like
jaime.virruetaf@gmail.com's avatar

Tykus, thank you very much for giving me your solution, you have helped me a lot, I want to post the final code, but I get an error that the time of inactivity passes ... although I have copied it from the notepad, I do not understand

Please or to participate in this conversation.