Named restful routes in Laravel 4

前端 未结 3 1279
情话喂你
情话喂你 2021-02-03 15:45

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

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

class UserController extends BaseController {
    p         


        
3条回答
  •  耶瑟儿~
    2021-02-03 16:01

    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.

提交回复
热议问题