4

So, I've been able to get restful controllers working with

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

class UserController extends BaseController {
    public function getAccount(){}
}

and so /users/account works. However if I try to do something like

Route::any('account',array('as' => 'account','uses' => 'UserController@account'));

and go to /account, it doesn't work (NotFoundHTTPException). Is there a way to use named routes and restful controllers in conjunction? I like how the restful system breaks up requests, and how named routes encapsulate the URI's and decouple them from the function names. This worked in Laravel 3. Am I missing something in the syntax, or did Laravel 4 purposefully disallow this kind of mix-and-match behavior? Thanks...

tereško
  • 58,060
  • 25
  • 98
  • 150
Osan
  • 187
  • 1
  • 3
  • 13

3 Answers3

20

This would depend entirely on the order you have defined the routes. If it's not working try reversing the order of the definitions.

But because Laravel is all about making your life easier you can pass an array of method names and their corresponding route name as the third parameter to Route::controller.

Route::controller('users', 'UsersController', ['getProfile' => 'user.profile']);

This might not directly apply to your situation but it is super handy.

Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
  • The array in the third parameter was exactly what I needed. That simplifies routing code a whole lot as well. Moral of the story, always check the function definitions in the Laravel code for undocumented extra parameters... – Osan May 31 '13 at 15:16
  • Perfect, thanks for exposing this undocumented functionality, I have namespaced my controllers so this removed a heap of unnecessary verbosity from my views – Erin Drummond Jul 31 '13 at 22:16
  • Just what I was looking for. However, I wish I could define this within the Controller@__construct() method. ... I'm guessing you can ;) *Off to the L4 docs for some mild reading. – Erik Aybar Mar 11 '14 at 22:07
1

Try this:

Route::get('/',array('as'=>'named_route','uses'=>'yourRestfulController@getMethod'));

This works nice for me. The trick was adding the action type after @ part. You should use the full name of the method unlike in L3.

Arda
  • 6,756
  • 3
  • 47
  • 67
0

This works nice for me. The trick was adding the action type after @ part. You should use the full name of the method unlike in L3.

Because REST prefix get, post, and so on are patterns to distinguish what type of REST it implements. When you named restful controllers route they didn't act like RESTful controllers anymore but a normal Controller you wish to named. Example of this:

Route::get('user/profile/', array('as'=>'dashboard', 'uses'=>'ProfileController@showDashboard'));

Consider this one: Assuming we want SystemController to be a RESTful controller so you'll define:

Route::controller('/', 'SystemController'); 

Then you want to named the postDashboard on the SystemController as dashboard, so you'll modified your routes as:

Route::get('user/profile/', array('as'=>'dashboard','uses'=>'SystemController@postDashboard'));
Route::controller('/', 'SystemController');

On that scenario,postDashboard should not be access via GET protocol since we declared it to bePOST, that is if Laravel treated it as RESTful Controller, since we named it that way it will be treated as normal not RESTful, so we can access it tru GET protocol. Naming it that way will be so dramatically not appropriate, coz we are breaking what we want first which is telling Laravel to treat SystemController as a RESTful.

I think you have to consider the post of Jason Lewis as the appropriate answer. No hard feelings @arda, since you are also correct.

lukaserat
  • 4,768
  • 1
  • 25
  • 36