簡體   English   中英

具有前綴、中間件和命名路由的管理路由組

[英]Admin route group with prefix, middleware and named routes

我在名為VehicleController的 controller 中有一些命名路線:

vehicle.index
vehicle.show

然后我有一個管理部分,在那里我定義了一個帶有前綴和中間件的路由組。 在本節中,我有一個資源 controller 名稱AdminVehicleController來處理Vehicle的 CRUD 任務(不確定這是否是最佳實踐),路徑如下:

vehicle.index
vehicle.create
vehicle.store
...

然而,這些命名路線是相互沖突的。 我的路線web.php現在看起來像這樣:

Route::get('vehicles', 'VehicleController@index')->name('vehicle.index');
Route::get('vehicle/{vehicle}', 'VehicleController@show')->name('vehicle.show');

Route::group(['prefix' => 'admin', 'middleware' => 'is.admin'], function () {
    Route::get('/', 'AdminDashboardController@index');
    Route::resource('vehicle', 'AdminVehicleController');
});

如果我將'name' => 'admin'添加到Route::group()數組中,則路線名稱將是adminvehicle.index而不是admin.vehicle.index

在路線中組合所有這些參數的正確方法是什么?

提供一個names數組作為第三個參數$options數組的一部分,每個鍵是資源 controller 方法(索引、存儲、編輯等),值是您要為路由提供的名稱。

    Route::resource('vehicle', 'AdminVehicleController', [
        'names' => [
            'index' => 'admin.vehicle.index',
            // etc...
        ]
    ]);

嘗試as您的管理員組的參數

Route::group(['prefix' => 'admin', 'middleware' => 'is.admin', 'as'=> 'admin.'], function () {
    Route::get('/', 'AdminDashboardController@index')->name('dashboard');
    Route::resource('vehicle', 'AdminVehicleController');
});

參考鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM