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

前端 未结 2 890
北海茫月
北海茫月 2020-12-13 16:03

I understand that a Resource controller can have the following methods

index
show
create
edit
store
update
destroy

Now suppose I have the f

2条回答
  •  天命终不由人
    2020-12-13 16:38

    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))
    

提交回复
热议问题