这个问题之前已经被问过了,我相信我的代码是正确的,但是我得到了奇怪的结果。
我需要根据一些数据库值在登录后将用户重定向到不同的路由。我认为我只需要将逻辑放在app/Http/Middleware/RedirectIfAuthenticated.php
文件中的handle
方法中即可完成。目前我的方法看起来像这样:
public function handle($request, Closure $next)
{
if ($this->auth->check()) {
if($this->auth->user()->sign_up_complete == 1){
return redirect('/');
} else {
if($this->auth->user()->step_one_complete == 0){
return redirect('/register/step-1');
} elseif($this->auth->user()->step_two_complete == 0){
return redirect('/register/step-2');
} else {
return redirect('/');
}
}
}
return $next($request);
}
这种方法不起作用,在登录后用户被重定向到/home
。我尝试将dd($this->auth->user())
放在$this->auth->check()
条件的内部,但它从未运行过。如果我将其放在该检查之外,则会在每个请求上运行。看起来$this->auth->check()
从未运行。
我的问题是:如果不是在这里,那么这个逻辑应该放在哪里?
我还从AuthController.php
控制器中删除了protected $redirectTo = '/account';
。
check()
函数在哪里? - Narendrasingh SisodiaRedirectIfAuthenticated
似乎是一个guest
中间件。此外,在Illuminate\Foundation\Auth\RedirectUsers
中,如果您没有设置$redirectTo
,则会重定向到/home
。但在这种情况下并不重要。您尝试过使用Authenticate
中间件吗?将逻辑移动到$this->auth->guest()
检查之后,然后再写您的内容。这个中间件将在用户需要进行身份验证时每次运行。因此,基本检查将在未经身份验证时将它们重定向。如果已经过身份验证,它将继续进行您的检查。 - Iamzozoguest
中间件,它会运行RedirectIfAuthenticated
。但是auth
中间件不会运行RedirectIfAuthenticated
,而是运行Authenticate
。因为您想在登录后检查用户是否完成了所有步骤,所以所有受保护的页面都将运行Authenticate
中间件,因此运行您的检查。如果您想从不需要的页面重定向用户,则使用guest
中间件。例如,如果用户已登录,则不希望显示登录表单。在这里,您使用guest
中间件,它将调用RedirectIfAuthenticated
并在已经登录时重定向到主页。 - Iamzozo中间件
的代码吗? - smartrahat