Laravel Route/Controllers (difference b/w APIresources and resources in route)

Laravel Route/Controllers (difference b/w APIresources and resources in route)

Normal Resource controller:

Route::resource('users', 'UsersController');

        

Gives you these 7 named routes:


Verb          Path                        Action  Route Name

GET           /users                      index   users.index
GET           /users/create               create  users.create
POST          /users                      store   users.store
GET           /users/{user}               show    users.show
GET           /users/{user}/edit          edit    users.edit
PUT|PATCH     /users/{user}               update  users.update
DELETE        /users/{user}               destroy users.destroy
        

API Resource controller:


Route::apiResource('users', 'UsersController');
        

Gives you these 5 named routes:


Verb          Path                        Action  Route Name

GET           /users                      index   users.index
POST          /users                      store   users.store
GET           /users/{user}               show    users.show
PUT|PATCH     /users/{user}               update  users.update
DELETE        /users/{user}               destroy users.destroy
        

To quickly generate an API resource controller that does not include the create or edit methods, use the --api switch when executing the make:controller command:


php artisan make:controller API/UsersController --api
        

Try using the command line to generate your controller. It will save you stress. You can then do this in your route


Route::apiResource('users', 'UsersController');
        

Thanks!!!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了