Laravel Resource Controllers

Laravel resource routing assigns the “CRUD” routes to a controller with the help of single line code.

For E.g.,

If we wish to create a controller that handles all HTTP requests “photos” stored by our application using the make:controller Artisan command. We can quickly create a controller.

php artisan make:controller PhotoController --resource

This above command will generate a controller at

app/Http/Controllers/PhotoController.php.

For each of the available resource operations, the controller will contain a method.

In the next step, we are going to register an original route to the controller:

Route::resource(‘photos’,‘PhotoController’);

The single route declaration creates many routes to handle different types of action on the resource. The generated controller will already have methods for these actions, including notes informing us to the HTTP verbs and URIs they handle.

We register many resource controllers at once by passing an array to the resource method:

Route::resource([
‘photos’ => ‘PhotoController’, 
‘posts’ => ‘PostController’
 ]); 

Actions Handled By Resource Controller

Verb URI Action Route Name
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show  photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photo/{photo} destroy photos.destory

Specifying the Resource Model:

If we are using route model binding and would like the resource controller`s methods to type-hint a model instance, we use the --model option when generating the controller:

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

Spoofing Form Methods:

Since the HTML forms can`t make PUT, PATCH, or DELETE requests, we will need to add a hidden _method field to spoof these HTTP verbs. The @method Blade directive can create this field for us:

<form action=”/foo/bar” method=”POST”>
 @method(‘PUT’) 
 </form> 
laravel resource controllers

The Resource Controllers are divided into 5 different types:

Partial Resource Route

When declaring a resource route, we specify a subset of actions.

The controller should handle instead of the face set of default actions:

Route::resource( ‘photos’,‘PhotoController’) ->only
([ 
‘index’, ‘show’ 
]); 
Route::resource( ‘photos’, ‘PhotoController’)-> except  
([
‘create’, ‘store’, ‘update’, ‘destroy’  
]); 

API Resource Routes:

When declaring resource routes that will be consumed by APIs, we will commonly want to exclude routes which present HTML templates like create and edit.

We use the apiResource method to exclude these two routes automatically:

Route::apiResource('photos','PhotoController');

We register many API resource controllers at once by passing an array to the apiResources method:

Route::apiResources([
'photos' => 'PhotoController', 
'posts' => 'PostController'
 ]); 

For quickly generate an API resource controller which does not include the create or edit methods, we use the --api switch for executing the make:controller command:

phpartisan make:controller API/PhotoController --api

Naming Resource Routes

By default, all type of resource controller actions have a route name. We can override these names by passing a names array with our options:

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

Naming Resource Route Parameters

The Route::resource will create the route parameters for our resource routes based on the “singularized” version of the resource name.

We can easily override this on resource basis by using the parameters method.

The array passed into the parameters method should be an associative array of resource names and parameters routes:

Route::resource(‘users’,‘AdminUserController’)->parameters
([ 
‘users’ => ‘admin_user’ 
 ]); 

The above example generates the following URIs for the resource`s show route:

/users/{admin_user}

Localizing Resource URIs

The Route::resource is used by default; it will create a resource URIs using the English verbs.

If we need to localize the create and edit action verbs, we can use the Route::resourceVerbs method.

It may be done in the boot method of our AppServiceProvider.

use Illuminate\Support\Facades\Route;

 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     Route::resourceVerbs([
         'create' => 'crear',
         'edit' => 'editar',
     ]);
 } 

Once the verbs have been customized, the resource route registration like Route::resource(‘fotos’, ‘PhotoController’) will produce the following URIs:

/fotos/crear
/fotos/{foto}/editar 

Supplementing Resource Controllers

If we need to add additional routes to a resource controller over the default set of resource routes, we should define these routes before our call to the Route::resource.

Otherwise, the routes defined by the resource method will take the lead over our supplement routes:  

Route::get(‘photos/popular’,‘PhotoController@method’);
Route::resource(‘photos’; ‘PhotoController’);