# 版本
# 前言
我喜歡使用 Laravel 開發的感覺, 除了開發快速, 程式碼簡潔且優雅之外, Laravel 框架本身也是一個很好的學習參照物。 本篇主要將官方文件重點整理成 Q&A 的形式呈現, 原子化的概念, 這方式並不適用於每個人, 但若對你有幫助, 我會很開心。
# 目錄
# Model Preparation
以下的 Laravel example code 的意思是?
- Example:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
- Answer: 在 User model 透過 implements
MustVerifyEmail
interface, 啟動 email verification feature
Laravel Email Verification 中, 當我在 User model implement MustVerifyEmail interface 之後, 每當註冊成功, Laravel 都會做什麼事?
會發確認 email 給該 user
# Database Considerations
Laravel Email Verification 中, 一旦 verify 成功, Laravel 會在哪個 column 記錄下 verified 的時間?
email_verified_at
# Routing
以下的 Laravel example code 的意思是?
- Example:
<?php
Auth::routes(['verify' => true]);
- Answer: 註冊使用內建的
Auth\VerificationController
# Protecting Routes
以下的 Laravel example code 的意思是?
- Example:
<?php
Route::get('profile', function () {
// Only verified users may enter...
})->middleware('verified');
- Answer: 存取 example.com/profile 的 request, 需是通過 email verified 的 user 方可存取
Laravel Email Verification 中, 驗證使用者是否 email verified 的 middleware 是哪一個?
Illuminate\Auth\Middleware\EnsureEmailIsVerified
# Views
Laravel Email Verification 中, email verification 的 view 可使用哪個 CLI 自動安裝?
composer require laravel/ui
php artisan ui vue --auth
Laravel Email Verification 中, 當我使用 CLI 安裝 authentication scaffolding 之後, email verification 的 view 定義在哪個檔案?
resources/views/auth/verify.blade.php
# After Verifying Emails
Laravel Email Verification 中, 當 email verification 完成後, 使用者會自動地被導向 /home, 若要自定義導向位置, 可在哪個檔案中的 protected $redirectTo property 定義?
VerificationController 中
以下位於 VerificationController 的 example code 的意思是?
- Example:
<?php
protected $redirectTo = '/dashboard';
- Answer: 一般來說, Email Verification 完成後, 預設會導向 home, 上面的 property $redirectTo 可以自定義重導向的位置
# Events
以下的 Laravel example code 的意思是?
- Example:
<?php
protected $listen = [
'Illuminate\Auth\Events\Verified' => [
'App\Listeners\LogVerifiedUser',
],
];
- Answer: 在 EventServiceProvider 中註冊一個 listener, 監聽
email verified
event