首页 > 解决方案 > 在 laravel 5.7 的验证电子邮件中更改邮件验证链接中的自定义参数的“VerifyEmail”类,用于方法 verifyUrl()

问题描述

laravel 5.7 自带的验证邮件。我需要如何以及在哪里更改它?我已经在网上搜索过,但是因为它是5.7中的全新功能,所以我找不到答案。你能帮我吗?提前致谢。

基本上那个类在 Illuminate\Auth\Notifications 下

我想覆盖其中一种方法:

 class VerifyEmail extends Notification
        {
          // i wish i could override this method
           protected function verificationUrl($notifiable)
            {
             return URL::temporarySignedRoute('verification.verify', Carbon::now()->addMinutes(60), ['id' => $notifiable->getKey()]);
            } 
        }

标签: laravelemail-verificationlaravel-5.7

解决方案


因为您的User模型使用Illuminate\Auth\MustVerifyEmail您可以覆盖该方法sendEmailVerificationNotification,该方法是通过调用该方法通知创建的用户notify并作为参数传递Notifications\MustVerifyEmail该类的新实例的方法。

您可以创建一个自定义通知,它将作为参数传递给模型$this->notify()中的sendEmailVerificationNotification方法User

public function sendEmailVerificationNotification()
{
    $this->notify(new App\Notifications\CustomVerifyEmail);
}

在您的CustomVerifyEmail通知中,您可以定义route处理验证的方式以及它将采用的所有参数。

当新用户注册时,Illuminate\Auth\Events\Registered会在 中发出一个事件,App\Http\Controllers\Auth\RegisterController并且该事件有一个Illuminate\Auth\Listeners\SendEmailVerificationNotification在 中注册的侦听器App\Providers\EventServiceProvider

protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ]
];

此侦听器检查在 Laravel 默认身份验证中$user作为参数传递给的是否是 Laravel 建议在模型中使用的特征的实例,当您想提供默认电子邮件验证并检查还没有已验证。如果所有这些都通过,它将调用该用户的方法:new Registered($user = $this->create($request->all()))App\Http\Controllers\Auth\RegisterControllerIlluminate\Contracts\Auth\MustVerifyEmailApp\User$usersendEmailVerificationNotification

if ($event->user instanceof MustVerifyEmail && !$event->user->hasVerifiedEmail()) {
        $event->user->sendEmailVerificationNotification();
}

推荐阅读