Laravel Controllers: Resource Controllers

Resource Controllers

Php artisan make:controller PhotoController --resource
Route::resource('photos', 'PhotoController');
Route::resources([ 'photos' => 'PhotoController', 'posts' => 'PostController' ]);
  • 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

Php artisan make:controller PhotoController --resource --model=Photo

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.

Route::apiResource('photos', 'PhotoController');
Route::apiResources([ 'photos' => 'PhotoController', 'posts' => 'PostController' ]);
Php artisan make:controller API/PhotoController --api

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

Route::resource('photos', 'PhotoController')->names([ 'create' => 'photos.build' ]);

Naming Resource Route Parameters

Route::resource('users', 'AdminUserController')->parameters([ 'users' => 'admin_user' ]);
/users/{admin_user}

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

<form action="https://laravel.com/foo/bar" method="POST">
    @method('PUT')
</form>

Laravel Controllers: Resource Controllers — Structure map

Clickable & Draggable!

Laravel Controllers: Resource Controllers — Related pages: