在 Laravel 5 中更改密码重置重定向 link

Change password reset redirect link in Laravel 5

我正在关注 this 关于使用 Laravel 设置身份验证的教程,除了一点点我大部分时间都在那里。

当我请求重设密码时,我会收到一封电子邮件,如果我点击电子邮件中的 link,我会看到一个表格,然后我会正确填写该表格并期望被重定向回 /dashboard,但是这永远不会发生,而是重定向到 /home。

我无法提供任何代码,因为一切都是由 Laravel 在幕后完成的。任何帮助表示赞赏:)。

对于Laravel 5.3,你必须在你的PasswordController中设置一个属性 redirectPath,当你使用内置函数时,用于登录后的重定向通过 Laravel.

应该是这样的:

# PasswordController.php

protected $redirectPath = '/dashboard';

由于 Laravel 5.4 属性 已重命名为 $redirectTo:

# PasswordController.php

protected $redirectTo = '/dashboard';

这是因为您希望重置密码控制器在成功重置后将您重定向到 /dashboard。也许我错了,但我在代码中的任何地方都看不到指定的地方(如您所说,基于全新的 Laravel 安装)。

为了演示这一点,让我们按照代码:

默认情况下,在全新 Laravel 安装中,您会在 app/Http/Controllers/Auth/ 目录中获得 PasswordController。在 line 21 of that file 上,它 'includes' ResetsPasswords 特征。那么让我们看看这个特征。

如您所见,在 postReset 方法中(即 运行 执行最终实际密码重置的方法),在 line 95 the redirect location is deferred to redirectPath, and as you can see, on line 131 上,代码专门转发用户如果控制器上不存在 redirectPathredirectTo 属性,则为 /home

因此,要手动设置重定向位置,只需在 Http\Controllers\Auth\PasswordController class.

中设置 protected $redirectPath = '/dashboard';

而且,在您链接到的页面上,请参阅 "After resetting passwords" 部分以获取有关此的官方文档。

如果有人在重定向时需要不同的地址(例如基于用户角色)可以将其添加到 ResetPasswordController

public function redirectPath()
    {
        if (auth()->user()->hasRole(xyz)) {
            return route('');
        }

        return route('');
    }

在 Laravel 5.6 中 $redirectPath 不是有效属性。请改用 $redirectTo

参见:Documentation

更改 Http\Controllers\Auth\ResetPasswordController 字段 redirectTo 的值:

protected $redirectTo = '/home';

如果您想使用动态路由,请从构造函数中确定该字段:

public function __construct()
{
    $this->redirectTo=route('home');
}

如果您使用的是 Spark,则需要覆盖 /spark/src/Http/Controllers/Auth/PasswordController.php 中的 getResetSuccessResponse()

为什么?

因为 /spark/src/Http/Controllers/Auth/PasswordController.php 使用具有 getResetSuccessResponse()ResetsPasswords trait 并且它重定向到来自 RedirectsUsers traitredirectPath(),它是 RedirectsUsers trait 的一部分Laravel 无法更改的框架。

不知何故这是忽略 $redirectTo 属性 并将用户发送到 /home 而不是 $redirectTo:

中声明的内容
public function redirectPath()
{
  if (property_exists($this, 'redirectPath')) {
    return $this->redirectPath;
  }

  return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

或者在某处创建 $redirectPath 属性。

希望这对某人有所帮助!