Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 19, 2022 04:29 am GMT

Customise Laravel Route for Resource Controller

When you create a controller, Laravel does provide you with the route resource which provides out of the box CRUD operation routes. You can check these named routes by the following command:

php artisan route:list

By default, following is the list of the photos controller CRUD operation routes:

VerbURIActionRoute Name
GET/photosindexphotos.index
GET/photos/createindexphotos.index
POST/photosindexphotos.index
GET/photos/{photo}indexphotos.index
GET/photos/{photo}/editindexphotos.index
PUT/PATCH/photosindexphotos.index
DELETE/photosindexphotos.index

Sometimes, we may want to use only few of the routes from the CRUD operation and let's say we want to use only index, create, store, edit and update, we can customise it like the following:

Route::resource('photos', 'PhotoController')->only('index', 'create', 'store', 'edit', 'update');

We can also specify the as option to define a prefix for every route name.

Route::resource('photos', 'PhotoController', [    'as' => 'foo']);

Similarly, you can also provide a custom name for each controller methods like the following:

Route::resource('photos', 'PhotoController', [    'names' => [        'index' => 'foo',        'store' => 'foo.new',        // etc...    ]]);

Original Link: https://dev.to/ibrarturi/customise-laravel-route-for-resource-controller-52j1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To