这是文档
When a user is successfully authenticated, they will be redirected to the
/home
URI. You can customize the post-authentication redirect location by defining aredirectTo
property on theLoginController
,RegisterController
, andResetPasswordController
:
protected $redirectTo = '/';
If the redirect path needs custom generation logic you may define a
redirectTo
method instead of aredirectTo
property:
protected function redirectTo() { // }
所以我定义了它
protected function redirectTo()
{
if (\Auth::user()->isAdmin()) {
return '/dashboard';
} else {
return '/home';
}
}
但正如您可能猜到的那样,它不起作用。它总是重定向到 /home
。
通过资源,我发现了这个
namespace Illuminate\Foundation\Auth;
trait AuthenticatesUsers
{
...
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
...
}
这是 $this->redirectPath()
的实现
namespace Illuminate\Foundation\Auth;
trait RedirectsUsers
{
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
}
我找不到它在哪里检查 redirectTo
方法。
顺便说一句,我正在使用 Laravel 5.3.28。
有什么建议吗?
编辑
这是在 5.3.29 中修复的,而我在 5.3.29 上。我仍然忍不住,但认为文档有问题,或者他们创建的这个 laravel
帮助程序来创建 Laravel 项目。我确实用它来生成项目,但它获取的不是最新版本。
最佳答案
改写 authenticated()
函数:
protected function authenticated($request,$user)
{
if(\Auth::user()->isAdmin()){
return redirect()->intended('dashboard');
}
return redirect()->intended('/home');
}
关于php - Laravel 登录后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506102/