如果尝试打开其他登录表单

时间:2017-10-27 02:03:14

标签: 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';
    }

此登录工作正常。但是,我遇到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);
    }

现在,如果用户已经登录,则仅当路由为/student-home而非/login时,才会将其重定向到/my-portal。即使我点击常规表格而不是我创建的这个额外表格。如果用户点击student-home

,如何重定向到/my-portal

2 个答案:

答案 0 :(得分:4)

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

Route::get('test', 'exampleController@example') ;

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

登录
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!
}

希望这能回答你的问题!

答案 1 :(得分:2)

请更改您的RedirectIfAuthenticated这样的中间件

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {

           if(guard == 'web_student') {
                return redirect('student-home');
             }else return redirect('/home');
        }
    return $next($request);
}

您的代码存在的问题是,如果用户已登录,则以下段将始终为true。您必须在此guard语句中检查是否已设置特定if你想要相应地重定向它们。

if (Auth::guard($guard)->check()) { return redirect('/home'); }