lancewan / blog Goto Github PK
View Code? Open in Web Editor NEW日志记录
日志记录
许多 web 应用要求用户在使用之前进行 Email 地址验证,Laravel提供了方便的方法来发送和验证电子邮件验证请求,下面我们来一步步实现这个功能。
laravel new laravel57
新建项目完成后,进入项目,查看 app/User.php
文件,Laravel5.7新增 Illuminate\Contracts\Auth\MustVerifyEmail
接口,该接口是实现用户登录或者注册成功后认证 Email 的功能,下面让 User 模型继承 Illuminate\Contracts\Auth\MustVerifyEmail
里面的抽象方法,该方法由 vendor/laravel/framework/src/Illuminate/Auth/MustVerifyEmail.php
Trait 实现:
修改 .env
文件数据库配置,链接数据库后迁移数据:
php artisan migrate
默认情况下,Laravel5.7 版本会在迁移数据后的 users
表中增加一个 email_verified_at
字段,该字段是用来存储邮箱验证日期,注册完成的用户没有验证邮箱的情况下这个字段为 null
上面已经知道 Laravel 为我们内置了 Email 认证的方法和数据字段,Laravel 中功能都是通过路由访问,Laravel 的Auth\VerificationController
类包含了发送验证链接和验证 Email 的所需逻辑。 要为这个控制器注册所需的路由 , 传递verify
项给Auth::routes
方法即可 :
Auth::routes(['verify' => true]);
此外,Laravel还提供了路由中间件来控制验证通过后的用户访问指定页面。Laravel 附带了verified
中间件,它定义在Illuminate\Auth\Middleware\EnsureEmailIsVerified
。由于此中间件已在 app/Http/Kernel.php
中注册,因此您需要做的就是将中间件附加到路由定义:
Route::get('/home', 'HomeController@index')->name('home')->middleware('verified');
执行make:auth
命令时,Laravel将生成邮箱验证需要的所有视图。 视图位于resources/views/auth/verify.blade.php
。您可以根据应用需要自定义此视图。
php artisan make:auth
完整路由如下:
true]);
Route::get('/home', 'HomeController@index')->name('home')->middleware('verified');
Email 验证是需要配置邮件服务器账号,关于邮箱配置发送邮件,大家可以看官方文档,本文章只为了展示 Email 验证的新特性,这里简单的把发送邮件的数据存到日志中,修改 .env
文件中邮件启动为 log
...
MAIL_DRIVER=log
...
在浏览器中打开项目页面注册用户,http://laravel57.test/register
(这里是我测试本地域名,大家按照自己配置即可)
注册用户成功后会直接跳到验证邮箱的页面:
前面我们配置有邮件驱动为log,所以我们在 storage/logs/laravel.log
日志中可以看到发送邮件的日志,其中有一段链接为激活邮件链接地址:
[Laravel](http://localhost)
# Hello!
Please click the button below to verify your email address.
Verify Email Address: http://laravel57.test/email/verify/2?expires=1536982370&signature=dd13bd8f92e1b2b0b3c059e8febd371b618c07816655034db5d20cd5c26d6bb9
If you did not create an account, no further action is required.
Regards,Laravel
If you’re having trouble clicking the "Verify Email Address" button, copy and paste the URL below
into your web browser: [http://laravel57.test/email/verify/2?expires=1536982370&signature=dd13bd8f92e1b2b0b3c059e8febd371b618c07816655034db5d20cd5c26d6bb9](http://laravel57.test/email/verify/2?expires=1536982370&signature=dd13bd8f92e1b2b0b3c059e8febd371b618c07816655034db5d20cd5c26d6bb9)
© 2018 Laravel. All rights reserved.
其中 Verify Email Address: http://laravel57.test/email/verify/2?expires=1536982370&signature=dd13bd8f92e1b2b0b3c059e8febd371b618c07816655034db5d20cd5c26d6bb9
就是激活 Email 邮件的链接地址,输入在浏览器中进入网页:
OK,Email验证到这里就结束了,描述不准确或者错误地方请指出,谢谢!
http://www.iwanli.me/post/ri-cheng/
走得慢但不后退...
http://www.iwanli.me/post/hello-gridea/
走得慢但不后退...
http://www.iwanli.me/post/flutter-tips/
走得慢但不后退...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.