邮箱激活流程梳理

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,
    ],
],
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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