Laravel Resource Controller Route Name
Feb 24, 2021
When you use a resource controller route, it automatically generates names for each individual route that it creates.
Example:
Route::resource('post', 'PostController');
It automatically generate routes with their name for us
POST | posts | posts.store
GET|HEAD | posts | posts.index
GET|HEAD | posts/create | posts.create
...
How we can modify automatically generated route names?
To modify name pass names
Route::resource('posts', 'PostController', [
'names' => [
'index' => 'all-posts',
'store' => 'post.new',
// etc...
]
]);
or
Specify the as
option to define a prefix for every route name.
Route::resource('posts', 'PostController', [
'as' => 'prefix'
]);
This will give you routes such as prefix.posts.index
, prefix.posts.store
, etc.