1. 安装
这个库很好用:jrean/laravel-user-verification,下载5.x版本是配套Laravel 5.5的。
在composer.json里添加:
"jrean/laravel-user-verification": "5.*"
然后:
composer update
2. 配置
修改 config/app 文件,在 providers 数组内追加如下内容:
'providers' => [
...
Jrean\UserVerification\UserVerificationServiceProvider::class,
],
修改 config/app 文件,在 aliases 数组内追加如下内容:
'aliases' => [
...
'UserVerification' => Jrean\UserVerification\Facades\UserVerification::class,
],
给用户表添加字段:
- verified: 用于表示用户是否已经验证过邮箱,1 表示已验证,0 表示未验证;
- verification_token: 发送给用户的 token。
php artisan make:migration add_verification_to_users_table --table=users
命令会在 database/migrations 目录下生成迁移文件 [timestamp]_add_verification_to_users_table.php, 修改迁移文件,修改后如下:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddVerificationToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('verification_token')->nullable();
$table->boolean('verified')->default(false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('verification_token');
$table->dropColumn('verified');
});
}
}
然后将迁移文件应用:
php artisan migrate
3. 定义邮件内容模板
新建一个模板文件 resources/views/emails/user-verification.blade.php:
Hey there,
Looks like this was the right email. :)
Next, please confirm your email address by clicking on the link below.
{{ $content['linkName'] }}
And that's it! You're that much closer to your xxx account.
See you there,
- xxx Team
4. 增加路由
我使用的dingoapi + passport做的api部分,所以我用:
$api->get('verification/{token}', 'UserController@verification');
也可以使用以下方式定义:
Route::get('verification/{token}', 'Auth\AuthController@getVerification');
5. 验证和调用
XXXController.php 文件,具体看你的路由:
只写了新增的部分,可以放在任何controller里。
use Jrean\UserVerification\Traits\VerifiesUsers;
use Jrean\UserVerification\Facades\UserVerification;
use Illuminate\Support\Facades\Mail;
class XXXController
{
use VerifiesUsers;
/**
* 发送验证邮件
* 调用:在你的用户注册逻辑中生成用户之后,调用该函数并传入User::create()的返回结果就好
*
* @param $user
*/
public function sendVerificationEmail($user)
{
// 生成用户的验证 token,并将用户的 verified 设置为 0
UserVerification::generate($user);
// 给用户发认证邮件
$params = [
'link' => url('api/verification', $user->verification_token) . '?email=' . urlencode($user->email),
'linkName' => 'Click Here'
];
$to = $user->email;
$subject = 'Welcome to XXX! Confirm Your Email';
Mail::send(
'emails.user-verification',
['content' => $params],
function ($message) use ($to, $subject) {
$message->to($to)->subject($subject);
}
);
}
/**
* 验证邮箱和token
*
* @param $token
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function verification($token)
{
$user = User::where('verification_token',$token)->first();
$user->verified = true;
$user->verification_token = null;
$user->save();
session()->flash('success', '恭喜您,邮箱验证成功!');
return redirect('https://XXX.com');
}
}
附:Laravel 5.5 自带SMTP邮件组件实现发送邮件 ← 这个是配合上面的邮件发送部分。
效果图:
The end.