Laravel 验证码认证学习记录小结(2)
此方法处理了用户提交表单后的逻辑,我们把重点放在 event(new Registered($user = $this->create($request->all())));,这里使用了 Laravel 的事件系统,触发了 Registered 事件。
打开 app/Providers/EventServiceProvider.php 文件,此文件的 $listen 属性里我们可以看到注册了 Registered 事件的监听器:
protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], ];
打开 SendEmailVerificationNotification 类,阅读其源码:vendor/laravel/framework/src/Illuminate/Auth/Listeners/SendEmailVerificationNotification.php
<?php
namespace Illuminate\Auth\Listeners;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Auth\MustVerifyEmail;
class SendEmailVerificationNotification
{
/**
* 处理事件
*
* @param \Illuminate\Auth\Events\Registered $event
* @return void
*/
public function handle(Registered $event)
{
// 如果 user 是继承于 MustVerifyEmail 并且还未激活的话
if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
// 发送邮件认证消息通知(认证邮件)
$event->user->sendEmailVerificationNotification();
}
}
}
可以看出 Laravel 默认已经为我们设置了邮件发送的逻辑,接下来我们来测试一下。
测试邮件
测试之前,我们先设置下邮件发送到 log 中,以便后面的测试:
修改环境设置文件.env
MAIL_DRIVER=log
邮件一般发在 storage/logs 目录下的日志文件中
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持黑区网络。
