DEV Community

Ibrar Hussain
Ibrar Hussain

Posted on

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
Enter fullscreen mode Exit fullscreen mode

In this article we will use photos controller as a example.

By default, following is the list of the ] 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');
Enter fullscreen mode Exit fullscreen mode

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

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

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...
    ]
]);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)