An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
February 19, 2022 04:29 am GMT
Original Link: https://dev.to/ibrarturi/customise-laravel-route-for-resource-controller-52j1
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:
Verb | URI | Action | Route Name |
---|---|---|---|
GET | /photos | index | photos.index |
GET | /photos/create | index | photos.index |
POST | /photos | index | photos.index |
GET | /photos/{photo} | index | photos.index |
GET | /photos/{photo}/edit | index | photos.index |
PUT/PATCH | /photos | index | photos.index |
DELETE | /photos | index | photos.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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To