Named restful routes in Laravel 4

狂风中的少年 提交于 2019-12-02 21:27:32

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!