且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何自Laravel 5.7定制电子邮件验证电子邮件?

更新时间:2023-01-25 23:21:59

当您想在 Laravel 5.7 中添加电子邮件验证时,建议的方法是实现Illuminate\Contracts\Auth\MustVerifyEmail并使用Illuminate\Auth\MustVerifyEmail特性在App\User模型上.

When you want to add Email Verification in Laravel 5.7 the suggested method is to implement Illuminate\Contracts\Auth\MustVerifyEmail and use the Illuminate\Auth\MustVerifyEmail trait on the App\User Model.

要进行一些自定义行为,您可以覆盖方法sendEmailVerificationNotification,该方法通过调用方法notify通知创建的用户,并作为参数传递Notifications\MustVerifyEmail类的新实例.

To make some custom behaviour you can override the method sendEmailVerificationNotification which is the method that notifies the created user by calling the method notify, and passes as a parameter a new instance of the Notifications\MustVerifyEmail class.

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

You can create a custom Notification which will be passed as a parameter to the $this->notify() within the sendEmailVerificationNotification method in your User Model:

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

...然后在CustomVerifyEmail通知中,您可以定义处理验证的方式.您可以通过发送带有自定义 verification.route 的电子邮件来通知创建的用户,该电子邮件将包含您想要的任何参数.

...then in your CustomVerifyEmail Notification you can define the way the verification will be handled. You can notify created user by sending an email with a custom verification.route which will take any parameters that you want.

电子邮件验证通知流程

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

When a new user signs-up an Illuminate\Auth\Events\Registered Event is emitted in the App\Http\Controllers\Auth\RegisterController and that Registered event has a listener called Illuminate\Auth\Listeners\SendEmailVerificationNotification which is registered in the App\Providers\EventServiceProvider:

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

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

The SendEmailVerificationNotification listener checks if the $user – which is passed as a parameter to new Registered($user = $this->create($request->all())) in the Laravel default authentication App\Http\Controllers\Auth\RegisterController – is an instance of Illuminate\Contracts\Auth\MustVerifyEmail which is the name of the trait that Laravel suggests is used in the App\User Model when you want to provide default email verification and also check that $user is not already verified. If all that passes, the sendEmailVerificationNotification method is called on that user:

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