【问题标题】:Laravel 5.3 - changing auth routes for reset passwordLaravel 5.3 - 更改重置密码的身份验证路由
【发布时间】:2017-03-14 20:11:12
【问题描述】:

在将 auth 的控制器移动到我的 Http/Controllers/Admin/Auth 目录,以及将 auth 的视图移动到 admin/auth 并像这样设置路由文件之后:

Route::group(['middleware' => 'web', 'prefix' => 'admin', 'namespace' => 'Admin'], function() {
    Auth::routes();
});

单击重置密码电子邮件中的链接时,我收到错误消息:

RouteCollection.php 第 161 行中的 NotFoundHttpException:

我在控制器中修改了 showResetForm 方法,如下所示:

    public function showResetForm(Request $request, $token = null)
    {
        return view('admin.auth.passwords.reset')->with(
            ['token' => $token, 'email' => $request->email]
        );
    }

这是php artisan route:list上列出的路线:

           POST      | admin/password/email             |                   | App\Http\Controllers\Admin\Auth\ForgotPasswordController@sendResetLinkEmail  | web,web,guest |
|        | POST      | admin/password/reset             |                   | App\Http\Controllers\Admin\Auth\ResetPasswordController@reset                | web,web,guest |
|        | GET|HEAD  | admin/password/reset             |                   | App\Http\Controllers\Admin\Auth\ForgotPasswordController@showLinkRequestForm | web,web,guest |
|        | GET|HEAD  | admin/password/reset/{token}     |                   | App\Http\Controllers\Admin\Auth\ResetPasswordController@showResetForm        | web,web,guest |
|        | GET|HEAD  | admin/register                   |                   | App\Http\Controllers\Admin\Auth\RegisterController@showRegistrationForm      | web,web,guest |
|        | POST      | admin/register                   |                   | App\Http\Controllers\Admin\Auth\RegisterController@register                  | web,web,guest

用户在邮件中收到的密码重置链接仍然指向没有管理员前缀的旧路由,我该如何更改?

【问题讨论】:

    标签: php laravel-5.3


    【解决方案1】:

    首先你必须覆盖Illuminate\Auth\Notifications\ResetPassword 类toMail 函数。将[URL_TO_RESETPWD] 替换为您的网址。

    namespace App\Notifications;
    
    use Illuminate\Auth\Notifications\ResetPassword;
    use Illuminate\Notifications\Messages\MailMessage;
    
    class AppResetPassword extends ResetPassword {
    
        // Overriding the function
        public function toMail($notifiable) {
            return (new MailMessage)
                ->line('You are receiving this email because we received a password reset request for your account.')
                ->action('Reset Password', [URL_TO_RESETPWD], $this->token))
                ->line('If you did not request a password reset, no further action is required.');
        }
    }
    

    下一步在您的管理员用户模型中覆盖CanResetPassword trait 的sendPasswordResetNotification 函数以使用您的上述类。

    public function sendPasswordResetNotification($token) {
        $this->notify(new App\Notifications\AppResetPassword\ResetPasswordNotification($token));
    }
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案2】:

      我遇到了这个问题,想使用我自己的纯文本电子邮件。

      添加到您的User 模型:

      public function sendPasswordResetNotification($token)
      {
          Mail::send(['text' => 'emails.password'], ['token' => $token], function ($message) {
              $message->subject(config('app.name').' Password Reset Link');
              $message->to($this->email);
          });
      }
      

      创建views/emails/password.blade.php:

      You are receiving this email because we received a password reset request for your account.
      
      Click here to reset your password: {{ url('auth/password/reset/'.$token) }}
      
      If you did not request a password reset, no further action is required.
      

      这将向他们发送一封纯文本密码重置电子邮件,并消除password.reset route not found 错误。

      【讨论】:

      • 对于任何希望根据路由名称生成 url 的人,您可以使用:route('password.reset', ['token' => {your token}])
      猜你喜欢
      • 2016-04-06
      • 1970-01-01
      • 2016-07-21
      • 2017-01-25
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多