2

When I add additional methods to a resource controller, like for example getHistory() or getStats(), the only way I can auto-detect them to avoid writing more routes is like that:

Route::controller('users','UsersController');
Route::resource('users','UsersController');

I believe the controller method will route only the prefixed methods and the resource method will not override them.

Is there not a better way to define custom routes according to additional methods inside a resource controller? Like an array as a parameter to it?

user2094178
  • 9,204
  • 10
  • 41
  • 70
  • You could do something like `Route::any('users', 'UsersController@route');`. And then have a `function route() {}` that switches the request method and any optional parameters or URI segments to call other `UsersController` functions. – Sam Jun 13 '13 at 21:25

1 Answers1

2

You correct in that controller methods have to be prefixed with http verb. Adding custom methods to controllers is as easy as this:

public function getCustom() {}
public function postCustom() {}

Resourceful controllers on the other hand are a little different. Adding additional method to those and having them auto-detected is more complex.

Defining the Route::controller() first followed by the Route::resource is the best way to have the best of both worlds; a resourceful api with custom routes.

I read a post the other day, about how you can add custom methods to resources. I'll try and find it then link you to it.

EDIT: Here is a link to another SO question similar that you may find helpful - https://stackoverflow.com/a/16661564/1233455

Community
  • 1
  • 1
JasonMortonNZ
  • 3,752
  • 8
  • 34
  • 56