Named restful routes in Laravel 4

前端 未结 3 1240
情话喂你
情话喂你 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.

    0 讨论(0)
  • 2021-02-03 16:13

    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.

    0 讨论(0)
  • 2021-02-03 16:19

    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.

    0 讨论(0)
提交回复
热议问题