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

前端 未结 2 892
北海茫月
北海茫月 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:43

    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.

提交回复
热议问题