How To Create Partial Resource Routes With Laravel

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Laravel resource controllers provide the CRUD routes to the controller in a single line of code. A resource controller is used to create a controller that handles all the http requests stored by your application. The resource() is a static function like get() method that gives access to multiple routes that we can use in a controller.

How To Create Partial Resource Routes With Laravel

Partial Resource Routes

When declaring a resource route, you may specify a subset of actions the controller should handle instead of the full 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, you will commonly want to exclude routes that present HTML templates such as create and edit. For convenience, you may use the apiResource method to automatically exclude these two routes:

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


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

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


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/PhotoController --api


To know more about creating resource controller visit our Bajarangisoft site.

 Create the Partial Resource Routes:


1: First, we create the demoController by using the below command:

php artisan make:controller demoController --resource


2: Now, we Create the following command in web.php file to create the Partial resource routes.
 
Route::resource('demo','demoController',['only' => ['create','show']]);  


3: To verify whether the above code has registered the routes for the specified methods or not, type the command 'php artisan route:list'.

Related Post