更改 Laravel 5 中的密码重置重定向链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33367666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:34:58  来源:igfitidea点击:

Change password reset redirect link in Laravel 5

phplaravelredirectpasswords

提问by Petar Vasilev

I am following thistutorial on setting up authentication with Laravel and I am mostly there apart from one bit.

我下面这个与Laravel设置身份验证的教程,我主要是有除了一位。

When I make a request for a password reset I get an email sent out to me and if I click on the link in the email I get to a form which I then fill in correctly and expect to be redirected back to /dashboard, however this never happens and instead it redirects to /home.

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

I can't provide any code as everything is done behind the scenes by Laravel. Any help appreciated : ).

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

回答by alexrussell

This is because you are expecting the reset password controller to redirect you to /dashboardon a successful reset. Maybe I'm wrong, but I can't see anywhere in the code where that is specified (based, as you say, on a fresh Laravel install).

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

To demonstrate this, let's follow the code:

为了证明这一点,让我们按照代码进行操作:

By default in a fresh Laravel installation you get a PasswordControllerin your app/Http/Controllers/Auth/directory. On line 21 of that file, it 'includes' the ResetsPasswordstrait. So let's look at that trait.

默认情况下,在全新的 Laravel 安装PasswordController中,您的app/Http/Controllers/Auth/目录中有一个。在该文件的第 21 行,它“包含”了ResetsPasswords特征。那么让我们来看看这个特性。

As you can see, in the postResetmethod (which is the method that is run to do the final actual password reset), on line 95the redirect location is deferred to redirectPath, and as you can see, on line 131the code specifically forwards the user to /homeif no redirectPathor redirectToproperty exists on the controller.

正如您所看到的,在postReset方法(这是运行以执行最终实际密码重置的方法)中,在第 95行重定向位置被推迟到redirectPath,并且如您所见,在第 131行代码专门转发用户到/home如果没有redirectPathredirectTo属性存在控制器上。

So, to manually set the redirect location, just set protected $redirectPath = '/dashboard';in your Http\Controllers\Auth\PasswordControllerclass.

因此,要手动设置重定向位置,只需protected $redirectPath = '/dashboard';在您的Http\Controllers\Auth\PasswordController类中设置即可。

But also, on the page you linked to, see the section "After resetting passwords"for the official docs on this.

而且,在您链接到的页面上,请参阅有关此官方文档的“重置密码后”部分。

回答by Tim

For Laravel 5.3 you have to set a property redirectPathin your PasswordControllerwhich is used for redirection after the login when you are using the built-in function by Laravel.

对于Laravel 5.3,你必须设置一个属性redirectPathPasswordController其中时,你使用的是登录后用于重定向内置的Laravel功能。

Should look like this:

应该是这样的:

# PasswordController.php

protected $redirectPath = '/dashboard';

Since Laravel 5.4 the property was renamed to $redirectTo:

从 Laravel 5.4 开始,该属性被重命名为$redirectTo

# PasswordController.php

protected $redirectTo = '/dashboard';

回答by Danny

If you're using Spark, you will need to override getResetSuccessResponse()in /spark/src/Http/Controllers/Auth/PasswordController.php.

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

Why?

为什么?

Because /spark/src/Http/Controllers/Auth/PasswordController.phpuses the ResetsPasswords traitwhich has the getResetSuccessResponse()and it redirects to the redirectPath()which comes from the RedirectsUsers traitwhich is part of the Laravel framework that can't be changed.

因为/spark/src/Http/Controllers/Auth/PasswordController.php使用ResetsPasswords trait具有 的getResetSuccessResponse()并且它重定向到redirectPath()来自RedirectsUsers traitLaravel 框架的不可更改的一部分。

Somehow this is ignoring the $redirectToproperty and sending the user to /homeinstead of what's been declared in $redirectTo:

不知何故,这忽略了该$redirectTo属性并将用户发送到/home而不是在 中声明的内容$redirectTo

public function redirectPath()
{
  if (property_exists($this, 'redirectPath')) {
    return $this->redirectPath;
  }

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

Or maybe just create the $redirectPathproperty somewhere.

或者也许只是在$redirectPath某处创建属性。

Hope this helps someone!

希望这对某人有帮助!

回答by despotbg

If someone need different address when redirect (based on e.g user role) can add this to ResetPasswordController

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

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

        return route('');
    }

回答by J. Robinson

In Laravel 5.6 $redirectPathis not a valid attribute. Use $redirectToinstead.

在 Laravel 5.6$redirectPath中不是一个有效的属性。使用$redirectTo来代替。

See: Documentation

请参阅:文档

回答by Luca C.

Change the value of field redirectToof Http\Controllers\Auth\ResetPasswordController:

更改字段的值redirectToHttp\Controllers\Auth\ResetPasswordController

protected $redirectTo = '/home';

if you want to use a dynamic route, valorize that field from the constructor:

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

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