1

我有这些路线:

Route::group(['middleware' => ['role:admin'], 'prefix' => 'admin'], function()
{
    Route::resource(__('route.events'), 'Auth\RoleAdmin\EventController');
});

Route::group(['middleware' => ['role:user'], 'prefix' => 'user'], function()
{
    Route::resource(__('route.events'), 'Auth\RoleUser\EventController');
});

我希望资源路由的名称分别以admin.和开头user.,但它不会发生。

这是命令的结果php artisan route:list

|        | GET|HEAD      | auth/admin/events                                           | events.index                            | App\Http\Controllers\Auth\RoleAdmin\EventController@index                        | web,auth,role
:admin,guest,role:admin|user                        |
|        | POST          | auth/admin/events                                           | events.store                            | App\Http\Controllers\Auth\RoleAdmin\EventController@store                        | web,auth,role
:admin,guest,role:admin|user                        |
|        | GET|HEAD      | auth/admin/events/create                                    | events.create                           | App\Http\Controllers\Auth\RoleAdmin\EventController@create                       | web,auth,role
:admin,guest,role:admin|user                        |
|        | DELETE        | auth/admin/events/{events}                                  | events.destroy                          | App\Http\Controllers\Auth\RoleAdmin\EventController@destroy                      | web,auth,role
:admin,guest,role:admin|user                        |
|        | GET|HEAD      | auth/admin/events/{events}                                  | events.show                             | App\Http\Controllers\Auth\RoleAdmin\EventController@show                         | web,auth,role
:admin,guest,role:admin|user                        |
|        | PUT|PATCH     | auth/admin/events/{events}                                  | events.update                           | App\Http\Controllers\Auth\RoleAdmin\EventController@update                       | web,auth,role
:admin,guest,role:admin|user                        |
|        | GET|HEAD      | auth/admin/events/{events}/edit                             | events.edit                             | App\Http\Controllers\Auth\RoleAdmin\EventController@edit                         | web,auth,role
:admin,guest,role:admin|user                        |

如您所见,管理员的路由和用户的路由具有相同的名称,并且名称不包含前缀。

4

2 回答 2

1

将您的路由更改为在组外具有前缀和中间件:

Route::prefix('admin')
->middleware('role:admin')
->group(function() {
    Route::resource(__('route.events'), 'Auth\RoleAdmin\EventController');
});

Route::prefix('user')
->middleware('role:user')
->group(function() {
    Route::resource(__('route.events'), 'Auth\RoleUser\EventController');
});

https://laravel.com/docs/6.x/routing#route-groups

于 2020-03-12T17:48:51.593 回答
1

kerbholz 的解决方案不起作用,但它帮助我解决了问题。

Route::prefix('admin')
    ->middleware('role:admin')
    ->name('admin.') // <-- I had to add this line in order to work
    ->group(function() {
        Route::resource(__('route.events'), 'Auth\RoleAdmin\EventController');
});
于 2020-03-12T19:36:27.483 回答