Laravel 4 - Route::resource vs Route::controller. Which to use?

柔情痞子 提交于 2019-11-28 18:25:51
Joseph Silber

Just use a resource controller, add those other methods to that same controller, and add routes to those methods directly:

Route::group(['prefix' => 'api'], function()
{
    Route::group(['prefix' => 'v1', 'namespace' => 'Api\V1'], function()
    {
        // Add as many routes as you need...
        Route::post('login', 'PostsResourceController@login');
        Route::get('find',   'PostsResourceController@find');
        Route::get('search', 'PostsResourceController@search');

        Route::resource('posts', 'PostsResourceController');
    });
});

P.S. I generally shy away from using Route::controller(). It's too ambiguous.

one of the problems associated with resource controllers are when you are using named routes, with group prefixes it all turns out into a big mess . if you want to make a small change in your prefix, you have to make changes throughout the views and controllers . ie you can't make full power of named routes.

i follow this model when developing my laravel apps .

Route::group( [ 'prefix' => 'admin' ], function(){
        Route::resource('pages', 'PageController', [
            'names' => [
                'show' => 'page',
                'edit' => 'page.edit'
            ],
            'only' => [
                'show',
                'edit'
            ]

        ]);

    });

so that i have the following advantages .

  • there are only routes that you need.
  • all the urls are clearly named

and i can generate urls comfortably using the syntax,even if i make a change in prefix or resource names urls are not affected

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