如果尝试打开其他登录表单,则RedirectIfAuthenticated重定向

Ste*_*eve 5 php laravel laravel-5.4

我有两个登录表单,有两个不同的表.一个是默认的/login路由,另一个是路由/myportal.我有额外的logincontroller

    protected $redirectTo = '/student-home';
    public function showLoginForm()
    {
        return view('my_portal');
    }

    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        return redirect('/my_portal');
    }

    protected function guard()
    {
        return Auth::guard('web_student');
    }

    public function username () 
    {
        return 'username';
    }
Run Code Online (Sandbox Code Playgroud)

此登录工作正常.但是,我遇到了问题RedirectIfAuthenticated

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        else if(Auth::guard('web_student')->check())
        {
            return redirect('student-home');
        }
        return $next($request);
    }
Run Code Online (Sandbox Code Playgroud)

现在,如果用户已经登录,它会被重定向到/student-home只有在路由/login,而不是/my-portal.即只有我点击常规表格而不是我创建的这个额外表格.student-home如果用户点击,我该如何重定向/my-portal

Nis*_*hah 4

您可以使用以下命令将控制器连接到 my-portal 路由:

Route::get('test', 'exampleController@example') ;
Run Code Online (Sandbox Code Playgroud)

然后在控制器功能中,您可以检查用户是否已经登录

public function example() {
    if(Auth::check()) {
        //This condition will run if the user is logged in !
        return redirect('student-home');
    }
    //Do whatever you want if user is not logged in!
}
Run Code Online (Sandbox Code Playgroud)

希望这能回答您的问题!