Home > front end >  How to add prefix to laravel route name
How to add prefix to laravel route name

Time:06-15

I am having my routes like this:

Route::resource('/admin/users', \App\Http\Controllers\AdminUsersController::class);
Route::resource('/admin/posts', \App\Http\Controllers\AdminPostsController::class);
Route::resource('/admin/categories', \App\Http\Controllers\AdminCategoriesController::class);
Route::resource('/admin/media', \App\Http\Controllers\AdminMediasController::class);
Route::resource('/admin/comment/replies', \App\Http\Controllers\CommentRepliesController::class);
Route::resource('/admin/comments', \App\Http\Controllers\PostCommentsController::class) ;

and my route list is

GET|HEAD        admin/categories .................... categories.index › AdminCategoriesController@index  
POST            admin/categories .................... categories.store › AdminCategoriesController@store  
GET|HEAD        admin/categories/create ........... categories.create › AdminCategoriesController@create  
GET|HEAD        admin/categories/{category} ........... categories.show › AdminCategoriesController@show  
PUT|PATCH       admin/categories/{category} ....... categories.update › AdminCategoriesController@update  
DELETE          admin/categories/{category} ......categories.destroy › AdminCategoriesController@destroy  
GET|HEAD        admin/categories/{category}/edit ...... categories.edit › AdminCategoriesController@edit  
GET|HEAD        admin/comment/replies ................... replies.index › CommentRepliesController@index  
POST            admin/comment/replies ................... replies.store › CommentRepliesController@store  
GET|HEAD        admin/comment/replies/create .......... replies.create › CommentRepliesController@create  
GET|HEAD        admin/comment/replies/{reply} ............. replies.show › CommentRepliesController@show
PUT|PATCH       admin/comment/replies/{reply} ......... replies.update › CommentRepliesController@update  
DELETE          admin/comment/replies/{reply} ....... replies.destroy › CommentRepliesController@destroy  
GET|HEAD        admin/comment/replies/{reply}/edit......... replies.edit › CommentRepliesController@edit  
GET|HEAD        admin/comments ........................... comments.index › PostCommentsController@index  
POST            admin/comments ........................... comments.store › PostCommentsController@store  
GET|HEAD        admin/comments/create .................. comments.create › PostCommentsController@create  
GET|HEAD        admin/comments/{comment} ................... comments.show › PostCommentsController@show  
PUT|PATCH       admin/comments/{comment} ............... comments.update › PostCommentsController@update  
DELETE          admin/comments/{comment} ............. comments.destroy › PostCommentsController@destroy  
GET|HEAD        admin/comments/{comment}/edit .............. comments.edit › PostCommentsController@edit  
GET|HEAD        admin/media .................................. media.index › AdminMediasController@index  
POST            admin/media .................................. media.store › AdminMediasController@store  
GET|HEAD        admin/media/create ......................... media.create › AdminMediasController@create  
GET|HEAD        admin/media/{medium} ........................... media.show › AdminMediasController@show  
PUT|PATCH       admin/media/{medium} ....................... media.update › AdminMediasController@update  
DELETE          admin/media/{medium} ..................... media.destroy › AdminMediasController@destroy  
GET|HEAD        admin/media/{medium}/edit ...................... media.edit › AdminMediasController@edit  
GET|HEAD        admin/posts ................................... posts.index › AdminPostsController@index  
POST            admin/posts ................................... posts.store › AdminPostsController@store  
GET|HEAD        admin/posts/create .......................... posts.create › AdminPostsController@create  
GET|HEAD        admin/posts/{post} .............................. posts.show › AdminPostsController@show  
PUT|PATCH       admin/posts/{post} .......................... posts.update › AdminPostsController@update  
DELETE          admin/posts/{post} ........................ posts.destroy › AdminPostsController@destroy  
GET|HEAD        admin/posts/{post}/edit ......................... posts.edit › AdminPostsController@edit  
GET|HEAD        admin/users ................................... users.index › AdminUsersController@index
POST            admin/users ................................... users.store › AdminUsersController@store  
GET|HEAD        admin/users/create .......................... users.create › AdminUsersController@create  
GET|HEAD        admin/users/{user} .............................. users.show › AdminUsersController@show  
PUT|PATCH       admin/users/{user} .......................... users.update › AdminUsersController@update  
DELETE          admin/users/{user} ........................ users.destroy › AdminUsersController@destroy  
GET|HEAD        admin/users/{user}/edit ......................... users.edit › AdminUsersController@edit  
GET|HEAD        api/user ............................................................................... 

is there a way to add prefix admin. to every single route name:

  • users.index would be admin.users.index
  • users.store would be admin.users.store

and so on for every resource route without doing this:

Route::resource('/admin/users', \App\Http\Controllers\AdminUsersController::class, ['names'=>['index'=>'admin.users.index']]);

CodePudding user response:

Surprised not to find a duplicate for this, but you can simply use a route group with the Router::name() method. Note I've done the same with the URL prefix using the Router::prefix() method:

use App\Http\Controllers as C;

Route::name("admin.")->prefix("admin")->group(function ($router) {
    Route::resource('users', C\AdminUsersController::class);
    Route::resource('posts', C\AdminPostsController::class);
    Route::resource('categories', C\AdminCategoriesController::class);
    Route::resource('media', C\AdminMediasController::class);
    Route::resource('comment/replies', C\CommentRepliesController::class);
    Route::resource('comments', C\PostCommentsController::class) ;
});

It's important to note that while the prefix() method is meant for a URL segment and so inserts the slash, name() is just a generic prefixer and won't add separators of any kind. This is why I included the dot in the name.


If you wish, you can use the $router parameter instead of the facade inside the closure. It is just a matter of personal preference which one you prefer. e.g.:

use App\Http\Controllers as C;

Route::name("admin.")->prefix("admin")->group(function ($router) {
    $router->resource('users', C\AdminUsersController::class);
    $router->resource('posts', C\AdminPostsController::class);
    $router->resource('categories', C\AdminCategoriesController::class);
    $router->resource('media', C\AdminMediasController::class);
    $router->resource('comment/replies', C\CommentRepliesController::class);
    $router->resource('comments', C\PostCommentsController::class) ;
});

CodePudding user response:

Route::group([
    'prefix' => __('router.product')
], function () {
    Route::get(__('router.products'), [ProductController::class, 'list'])->name('admin.product.list');
    Route::get(__('router.product-create'), [ProductController::class, 'create'])->name('admin.product.create');
    Route::post(__('router.product-create'), [ProductController::class, 'createDo'])->name('admin.product.createDo');
});
  • Related