1

I need to add an additional method (POST search) to the routes that are added as a resource, can someone guide me on how I can achieve it?

In the routes.php file I have the following code:

Route::resources([
    '/user' => App\Http\Controllers\UserTC::class,
    '/role' => App\Http\Controllers\RoleTC::class,
    ...
]);

Somehow I want the search method to be used when setting route::resources such as index, show, store, etc.

Thanks

Edit 1: What I'm looking for is to prevent doing this:

Route::post('/user/search', [App\Http\Controllers\UserTC::class, 'search']);
Route::post('/role/search', [App\Http\Controllers\RoleTC::class, 'search']);
...etc
pedroPG
  • 123
  • 7
  • And what didn't work exactly? Where is the said `search` method in your code? – dbf Jan 10 '23 at 20:11
  • It's a method that will perform a search, but I don't want to add a route for every controller in my project, so I need the method to be included as a resource. – pedroPG Jan 10 '23 at 20:13
  • So to word it differently, you would like to register a route for some resource (as in a search route) automatically? If so how? As a relay? Automatic registration? Or .. ? What would be your ideal amount of effort to handle this problem? – dbf Jan 10 '23 at 20:22
  • 1
    There is no way to do what you want, you must manually write each `search` route, or use a `foreach` or something similar and when iterating, writting that `Route::post` search – matiaslauriti Jan 10 '23 at 20:27
  • I only want the route for the controller to be generated automatically (and displayed with php artisan route:list) everything else will be hardcoded. – pedroPG Jan 10 '23 at 20:28
  • If `search` is always going to part of your resource, see [this answer](https://stackoverflow.com/a/36170234/1653305), should still be valid in Laravel 9. – dbf Jan 10 '23 at 21:09
  • you would have to extend the ResourceRegistrar and use your own instance to register the routes ... years ago but thats the concept: https://asklagbox.com/blog/resource-registrar – lagbox Jan 11 '23 at 03:28

0 Answers0