Laravel Resource Controller Routes
October 18, 2024 ยท AI Conversations
User
Laravel resource controller add prefix to route names
Assistant
In Laravel, to add a prefix to route names for a resource controller, you can use the `names` option when defining the resource route. Here's how you can add a prefix to the names of routes:
### Example:
Route::resource('admin/posts', PostController::class)->names([
'index' => 'admin.posts.index',
'create' => 'admin.posts.create',
'store' => 'admin.posts.store',
'show' => 'admin.posts.show',
'edit' => 'admin.posts.edit',
'update' => 'admin.posts.update',
'destroy' => 'admin.posts.destroy',
]);
This will apply the prefix `admin.posts` to the resource route names.
Alternatively, if you want to apply the prefix to all resource routes within a route group, you can define a route group with the `as` option:
### Example using a route group:
Route::prefix('admin')->name('admin.')->group(function () {
Route::resource('posts', PostController::class);
});
This will automatically prefix all the route names of the `posts` resource with `admin.`, making them `admin.posts.index`, `admin.posts.create`, etc.
### Generated route names:
– `admin.posts.index`
– `admin.posts.create`
– `admin.posts.store`
– `admin.posts.show`
– `admin.posts.edit`
– `admin.posts.update`
– `admin.posts.destroy`