邮箱激活流程梳理

1、新建判断邮箱是否激活中间件

php artisan make:middleware CheckIfEmailVerified

如果$user->email_verified为false,则邮箱没有激活,跳转激活提示页

2、注册中间件
Kernel.php中

protected $routeMiddleware = [
'email_verified' => \App\Http\Middleware\CheckIfEmailVerified::class,
];

3、配置路由

Route::group(['middleware' => 'auth'], function(){
    Route::group(['middleware' => 'email_verified'], function(){
        Route::get('/test', function(){
            return 'your email is verified';
        })
    });
});

4、创建邮件通知类

php artisan make:notification EmailVerificationNotification

在通知类中,申明里加上了implements ShouldQueue, ShouldQueue接口本身没有什么意义,实现了ShouldQueue的类会放进队列里实现异步发送;
greeting()方法设置邮件欢迎词;
subject()设置邮件标题;
line()在邮件里添加一行文字;
action()在内容里添加一个激活连接;

5、创建激活链接

php artisan make:controller EmailVerificationController

publi function verify中的逻辑:$email,$token是否存在、$token是否正确、email在数据库中是否存在
设置激活链接路由Route::get('/email_verification/verify', 'EmailVerificationController@verify')->name('email_verification');

6、将激活链接添加到发送邮件通知类中

7、在EmailVerificationController中添加send()方法主动发送激活邮件
$user->notify(new EmailVerificationNotification());

8、创建注册事件监听器

php artisan make:listener RegisteredListener
$user->notify(new EmailVerificaitonNotification());

9、将事件和监听器关联
在app/Providers/EventServiceProvider.php中

protected $listen = [
    Registered::class => [
        RegisteredListener::class,
    ],
],
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!