【问题标题】:Redirect after sending password reset link in Laravel 5.1?在 Laravel 5.1 中发送密码重置链接后重定向?
【发布时间】:2015-12-25 01:56:57
【问题描述】:

发送密码重置链接后如何设置重定向路径?

ResetsPaswords trait 中是这样的代码:

switch ($response) {
    case Password::RESET_LINK_SENT:
       return redirect()->back()->with('status', trans($response));

    case Password::INVALID_USER:
       return redirect()->back()->withErrors(['email' => trans($response)]);
}

但我不想更改供应商文件。还有其他方法吗?

【问题讨论】:

    标签: php authentication redirect laravel-5.1


    【解决方案1】:

    以下内容对我有用,请将以下内容添加到您的ForgotPasswordController

    protected function sendResetLinkResponse(Request $request, $response)
    {
        return redirect()->route('login')->with('status', trans($response));
    }
    

    【讨论】:

      【解决方案2】:

      您会在ResetsPasswords trait 中注意到以下两个函数:getSendResetLinkEmailSuccessResponsegetSendResetLinkEmailFailureResponse,它们在发送密码重置电子邮件后处理重定向,具体取决于重置电子邮件是否发送成功。

      更改此设置的一个很好的理由是,如果(在我的情况下)您希望在发送密码重置链接后将用户返回到主登录屏幕。

      为此,只需更改您的 App\Http\Controllers\Auth\PasswordController 并覆盖该函数,如下所示:

      protected function getSendResetLinkEmailSuccessResponse($response)
      {
          return redirect()->route('login')->with('status', trans($response));
      }
      

      【讨论】:

      • 我假设您使用的是 Laravel 通过 make:auth 命令创建的 PasswordController?并且它使用ResetsPassword 特征?你使用的是哪个版本的 Laravel?
      • 我使用的是 Laravel 5.1(见标题),但我不记得使用 make::auth 命令
      • 您在App\Http\Controllers\Auth\PasswordController 中有PasswordController.php 吗?你能粘贴整个文件吗?
      • 我有 Laravel 5.1 附带的默认 PasswordController.php,我在 function __construct() 下添加了您的代码
      • 我用这个代码得到了Route [login] not defined,尽管php artisan route:list 显示我的登录路径正常。 return redirect('login')->with('status', trans($response)); 有效,但您需要将密码/邮件模板中的 session('status') 部分复制到登录模板中才能看到消息。 Laravel 5.2,使用 make:auth.
      【解决方案3】:

      您无需编辑供应商文件。只需打开 /app/Http/Controllers/Auth/PasswordController.php 并在 PasswordController 类中添加以下代码行

          protected $redirectPath = '/DesiredUrl';
      

      在 Laravel 5.1 上测试

      【讨论】:

      • 这会在点击重置链接并且用户更改密码后更改重定向。问题是在重置链接发送后如何更改重定向。
      【解决方案4】:

      正如您在 trait 代码中看到的,成功的结果会重定向回相同的路由:

      redirect()->back()->with('status', trans($response));
      

      请注意with() 方法中的status 变量。这会设置一个临时会话变量,然后您可以在视图模板中使用该变量,例如通知用户邮箱设置成功。

      @if (session('status'))
      
      // ... notification that the email has been sent
      
      @else
      
      // ... your original form for submitting an email address
      
      @endif
      

      关键是,通常不需要为了告诉用户电子邮件已发送而重定向到另一条路线。您可以对相同的视图使用相同的路由,并在视图中创建两个代码块。一个生成提交电子邮件地址的表单,另一个生成成功通知。

      【讨论】:

      • 不确定为什么这是公认的答案。这并不能提供解决方案。
      【解决方案5】:

      您可以通过将protected $redirectTo = '/dashboard'; 添加到您的PasswordController 来做到这一点。

      所以你的控制器看起来像这样:

      <?php
      
      namespace App\Http\Controllers\Auth;
      
      use App\Http\Controllers\Controller;
      use Illuminate\Foundation\Auth\ResetsPasswords;
      
      class PasswordController extends Controller
      {
          /*
          |--------------------------------------------------------------------------
          | Password Reset Controller
          |--------------------------------------------------------------------------
          |
          | This controller is responsible for handling password reset requests
          | and uses a simple trait to include this behavior. You're free to
          | explore this trait and override any methods you wish to tweak.
          |
          */
      
          use ResetsPasswords;
      
          protected $redirectTo = '/dashboard';
      
          //The rest of the controller below this...
      

      您可以通过将/dashboard 更改为您希望将它们重定向到的位置来自定义路由。

      您应该查看docs 了解更多信息。

      【讨论】:

      • 这个重定向是在密码重置之后,问题是发送激活链接后如何重定向(插入您的电子邮件后)...
      猜你喜欢
      • 1970-01-01
      • 2016-01-26
      • 2017-10-21
      • 2014-11-23
      • 2015-07-18
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2019-02-07
      相关资源
      最近更新 更多