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 …


This content originally appeared on DEV Community and was authored by Ibrar Hussain

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...
    ]
]);


This content originally appeared on DEV Community and was authored by Ibrar Hussain


Print Share Comment Cite Upload Translate Updates
APA

Ibrar Hussain | Sciencx (2022-02-19T04:29:08+00:00) Customise Laravel Route for Resource Controller. Retrieved from https://www.scien.cx/2022/02/19/customise-laravel-route-for-resource-controller/

MLA
" » Customise Laravel Route for Resource Controller." Ibrar Hussain | Sciencx - Saturday February 19, 2022, https://www.scien.cx/2022/02/19/customise-laravel-route-for-resource-controller/
HARVARD
Ibrar Hussain | Sciencx Saturday February 19, 2022 » Customise Laravel Route for Resource Controller., viewed ,<https://www.scien.cx/2022/02/19/customise-laravel-route-for-resource-controller/>
VANCOUVER
Ibrar Hussain | Sciencx - » Customise Laravel Route for Resource Controller. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/19/customise-laravel-route-for-resource-controller/
CHICAGO
" » Customise Laravel Route for Resource Controller." Ibrar Hussain | Sciencx - Accessed . https://www.scien.cx/2022/02/19/customise-laravel-route-for-resource-controller/
IEEE
" » Customise Laravel Route for Resource Controller." Ibrar Hussain | Sciencx [Online]. Available: https://www.scien.cx/2022/02/19/customise-laravel-route-for-resource-controller/. [Accessed: ]
rf:citation
» Customise Laravel Route for Resource Controller | Ibrar Hussain | Sciencx | https://www.scien.cx/2022/02/19/customise-laravel-route-for-resource-controller/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.