如何為 Laravel 路由名稱新增前綴
P粉760675452
P粉760675452 2023-11-09 13:50:35
0
1
633

我的路線是這樣的:

Route::resource('/admin/users', AppHttpControllersAdminUsersController::class);
Route::resource('/admin/posts', AppHttpControllersAdminPostsController::class);
Route::resource('/admin/categories', AppHttpControllersAdminCategoriesController::class);
Route::resource('/admin/media', AppHttpControllersAdminMediasController::class);
Route::resource('/admin/comment/replies', AppHttpControllersCommentRepliesController::class);
Route::resource('/admin/comments', AppHttpControllersPostCommentsController::class) ;

我的路線清單是

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 ...............................................................................

有沒有辦法為每個路由名稱加上前綴admin.

  • users.index 將是 admin.users.index
  • users.store 將會是 admin.users.store

對每個資源路由依此類推,而不執行此操作:

Route::resource('/admin/users', AppHttpControllersAdminUsersController::class, ['names'=>['index'=>'admin.users.index']]);


#
P粉760675452
P粉760675452

全部回覆(1)
P粉460377540

很驚訝沒有找到重複的內容,但您可以簡單地使用帶有 Router::name() 方法。請注意,我使用 對 URL 前綴執行了相同的操作Router::prefix()方法

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) ;
});

需要注意的是,雖然prefix() 方法適用於URL 段,因此會插入斜杠,但name() 只是一個通用前綴並贏得不要添加任何類型的分隔符號。這就是為什麼我在名稱中包含點。


如果您願意,可以使用 $router 參數而不是閉包內的外觀。您喜歡哪一個只是個人喜好問題。例如:

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) ;
});
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!