首页 文章

更改邮件中验证链接中自定义参数的“VerifyEmail”类,以便在laravel 5.7中的验证电子邮件中进行方法验证Url()

提问于
浏览
3

与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()]);
            } 
        }

1 回答

  • 4

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

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

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

    在您的 CustomVerifyEmail 通知中,您可以定义 route ,通过该处理将验证验证及其将采用的所有参数 .

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

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

    此侦听器检查 $user (在Laravel默认身份验证 App\Http\Controllers\Auth\RegisterController 中作为参数传递给 new Registered($user = $this->create($request->all())) )是 Illuminate\Contracts\Auth\MustVerifyEmail 的实例,这是Laravel建议在 App\User 模型中使用的特征,当您要提供默认电子邮件验证和检查时还有 $user 尚未经过验证 . 如果所有通过它将调用该用户的 sendEmailVerificationNotification 方法:

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

相关问题