我正在使用 Laravel 项目版本 5.8。
我使用了两个中间件Admin和Author.
我已经创建了AdminMiddleware和AuthorMiddlware。
我都在Kernel.php. 我改变了RedirectIfAuthenticated,loginController用于自定义重定向。我创建了不同的路线组。
对于管理中间件,它将重定向admin.dashboard,对于作者中间件,它将重定向到author.dashboard.
之后,它会重定向到/home默认 Laravel 登录用户的路由。我想在登录后将管理员重定向到 admin.dashboard,将作者重定向到author.dashboard。自定义重定向不起作用。我多次查看了我的项目,但找不到问题。
登录控制器.php
// protected $redirectTo = '/home';
public function __construct(){
if(Auth::check() && Auth::user()->role->id==1) {
return redirect()->route('author.dashboard');
} else if (Auth::check() && Auth::user()->role->id==2) {
return redirect()->route('admin.dashboard');
}else {
$this->middleware('guest')->except('logout');
}
}
Run Code Online (Sandbox Code Playgroud)
RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null){
if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
return redirect()->route('author.dashboard'); …Run Code Online (Sandbox Code Playgroud)