Laravel 5.3:错误重定向了你太多次了

时间:2017-09-20 08:00:54

标签: php laravel laravel-5.3

我在尝试导航到" / 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'));
}

提前谢谢!

1 个答案:

答案 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');
}