laravel5.5安装不细述,直接进入正题。
1、配置好数据库信息之后,配置用户表 database/migrations/xxx_create_table.php,添加邮箱认证所需的信息
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('confirmation_token');
$table->smallInteger('is_active')->default(0);
$table->rememberToken();
$table->timestamps();
});
2、执行命令,构建laravel整个认证系统
php artisan make:auth
php artisan migrate
此时数据库中应该有3个表,如下:
3、sendcloud驱动已有laravel版本,git地址:https://github.com/NauxLiu/Laravel-SendCloud
使用composer安装,laravel5.5已自动注册,不要更改 config/app.php,详细配置请参考git。
composer require naux/sendcloud
4、在sendcloud注册账号,得到api_user和api_key,并认证你的域名等信息,新建邮件模板:
5、现在进行邮箱认证的逻辑设计,大概思路如下:
具体实现:app/Http/Controllers/Auth/RegisterController.php
<?php namespace App\Http\Controllers\Auth; use App\User; use Mail; use Naux\Mail\SendCloudTemplate; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return \App\User */ protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'confirmation_token' => str_random(40), 'password' => bcrypt($data['password']), ]); $this->sendVerifyEmailTo($user); return $user; } private function sendVerifyEmailTo($user) { $data = [ 'url' => route('email.verify', ['token' => $user->confirmation_token]), 'name' => $user->name ]; $template = new SendCloudTemplate('app_user_register', $data); Mail::raw($template, function ($message) use ($user){ $message->from('xxxx@xxx.com', 'Laravel'); $message->to($user->email); }); } }
增加fillable内容--confirmation,否则数据更新失败,app/User.php
protected $fillable = [ 'name', 'email', 'password', 'confirmation_token', ];
配置 routes/web.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); //注册用户点击验证邮箱 Route::get('email/verify/{token}', ['as' => 'email.verify', 'users' => 'EmailController#verify']);
新建路由对应的控制器EmailController,用来对注册用户点击验证邮箱之后的操作
php artisan make:controller EmailController
app/Http/Controllers/Auth/RegisterController.php
<?php namespace App\Http\Controllers; use Auth; use App\User; use Illuminate\Http\Request; class EmailController extends Controller { /** * @param $token * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ protected function verify($token) { // 在注册用户表中查找对应的token $user = User::where('confirmation_token', $token)->first(); // 如果是空,则跳转到主页 if (is_null($user)) { // return redirect('/' ); } // 如果非空,则置is_active为1,重置token,防止反复验证,并保存到用户数据库中,自动登录,最后跳转到用户中心 $user->is_active = 1; $user->confirmation_token = str_random(40); $user->save(); \Auth::login($user); return redirect('/home'); } }
效果如下:
点击验证之后:
----------------------------------------
end!