我在尝试导航到" / admin"时遇到此错误。其他路线如" / employee"工作正常。
以下是我目前的网络路线
Auth::routes();
/* Voyager Routes */
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
...
});
/* Badge App Routes - All the dashboard routes for managers, employees and HRs are defined here */
Route::group(['middleware' => 'auth', 'prefix' => 'employee'], function () {
Route::get('/', 'frontend\DashboardController@index')->name('homepage');
Route::get('dashboard', 'frontend\DashboardController@index')->name('homepage');
...
});
Route::group(['middleware' => 'auth'], function () {
Route::resource('team-manager', 'frontend\TeamManagerController');
Route::resource('badges', 'backend\BadgeController');
Route::get('badges/award/{id?}', 'backend\BadgeController@award');
Route::post('store_award', 'backend\BadgeController@storeAward')->name('store_award');
});
/* User Redirector - Based on user role */
Route::group(['middleware' => ['redirector']], function () {
Route::get('/');
Route::get('login');
});
这是我的中间件重定向器
public function handle($request, Closure $next){
if (!Auth::guest()) {
$user = User::find(Auth::id());
// TODO: fix static id below
return $user->role_id == 1 ? redirect('admin') : redirect('employee');
}
return redirect(route('voyager.login'));
}
提前谢谢!
答案 0 :(得分:1)
问题在于您的中间件:
return $user->role_id == 1 ? redirect('admin') : redirect('employee');
您有管理员角色,而且您也在/admin
页面。然后,您的中间件会一次又一次地重定向到/admin
。
最好检查用户是否不在/admin
或/admin/*
相关路线中,然后将其重定向到管理员。
if($user->role_id == 1) {
//check if user is in /admin or /admin related routes.
return ($request->is('/admin') or $request->is('/admin/*')) ? $next($request) : redirect('admin');
} else {
redirect('/employee');
}