php - Laravel 登录后重定向

标签 php laravel authentication laravel-5.3

这是文档

When a user is successfully authenticated, they will be redirected to the /home URI. You can customize the post-authentication redirect location by defining a redirectTo property on the LoginController, RegisterController, and ResetPasswordController:

protected $redirectTo = '/';

If the redirect path needs custom generation logic you may define a redirectTo method instead of a redirectTo 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/

相关文章:

authentication - 无法登录到 mailgun 帐户。 "The CSRF session token is missing."

java - IBM Watson APIs Java SDK 使用 Watson token 认证失败

ruby-on-rails - Ruby on Rails OAuth应用程序可在客户端上运行,但不能在服务器上运行(OAuth::Unauthorized(401未经授权))

php - 在php中获取上个月的日期

php - Mysqli 事务查询优于普通 php mysqli 查询

php - mysql_fetch_row 会遇到连接错误吗?

php - 需要一段日期时间的第一天和最后一天

php - 为什么 Laravel 不加载使用 Laravel Breeze Starter Kit 安装的 Tailwind css 样式?

php - 如何修改 Laravel Jetstream 登录

php - 我应该将对象或普通数据传递到 View 中吗?