DEV Community

Ibrar Hussain
Ibrar Hussain

Posted on

5 1

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs