Skip to content
Advertisement

Laravel custom action names and route names using a resource controller?

By default Resource controller gives you those Actions and Route names:

ACTION  ROUTE NAME
index   resource.index
create  resource.create
store   resource.store
show    resource.show
edit    resource.edit
update  resource.update
destroy resource.destroy

and want to rename both of them (Actions and Routes names) to:

ACTION  ROUTE NAME
**browse    resource.browse**
create  resource.create
store   resource.store
show    resource.show
edit    resource.edit
update  resource.update
**delete    resource.delete**

And still use a Resource Controllers, like this:

Route::resource('resource', 'ResourceController');

and not a list of GET routes like this:

Route::get('resource', 'ResourceController@index');
Route::get('resource/create', 'ResourceController@index');
...

Advertisement

Answer

Here is a good solution suggested by Laravel doc:

Route::resource('resource', 'ResourceController', ['names' => [
    'index' => 'resource.browse',
    'delete' => 'resource.delete',
]]);

The rest will have the default names.

Related section in Laravel docs: https://laravel.com/docs/5.2/controllers#restful-naming-resource-routes

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement