Laravel Controllers: Resource Controllers
Resource Controllers
- You may register a resourceful route to the controller.
- You may register many resource controllers at once by passing an array to the resources method.
- Resource routing assigns the typical "CRUD" routes to a controller with a single line of code.
Actions Handled By Resource Controller
Specifying The Resource Model
If you are using route model binding and would like the resource controller's methods to type-hint a model instance, you may use the --model option when generating the controller.
Partial Resource Routes
Route::resource('photos', 'PhotoController')->only([ 'index', 'show' ]); Route::resource('photos', 'PhotoController')->except([ 'create', 'store', 'update', 'destroy' ]); When declaring a resource route, you may specify a subset of actions the controller should handle instead of the full set of default actions.
API Resource Routes
When declaring resource routes that will be consumed by APIs, you will commonly want to exclude routes that present HTML templates such as create and edit.
You may register many API resource controllers at once by passing an array to the apiResources method.
O 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.
Naming Resource Routes
Naming Resource Route Parameters
Localizing Resource URIs
Use Illuminate\Support\Facades\Route; /**
* Bootstrap any application services.
*
* @return void
*/ public function boot()
{ Route::resourceVerbs([ 'create' => 'crear', 'edit' => 'editar', ]); } If you need to localize the create and edit action verbs, you may use the Route::resourceVerbs method.
Supplementing Resource Controllers
Route::get('photos/popular', 'PhotoController@method');
Route::resource('photos', 'PhotoController'); If you need to add additional routes to a resource controller beyond the default set of resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes.
Spoofing Form Methods
Related concepts
- Resource Controllers: Actions Handled By Resource Controller
- Resource Controllers: Specifying The Resource Model
- Resource Controllers: Partial Resource Routes
- Resource Controllers: API Resource Routes
- Resource Controllers: Naming Resource Routes
- Resource Controllers: Naming Resource Route Parameters
- Resource Controllers: Localizing Resource URIs
- Resource Controllers: Supplementing Resource Controllers
- Resource Controllers: Spoofing Form Methods
Semantic portal