Laravel实现邮件验证需三处关键配置:一是User模型实现MustVerifyEmail接口并确保users表含email_verified_at字段;二是路由中调用Auth::routes(['verify' => true])注册验证路由;三是受保护路由必须同时使用auth和verified中间件。
Laravel 实现邮件验证激活账户,核心是启用 MustVerifyEmail 接口并配对路由、通知和数据库字段 —— 但光 implements 接口不会自动发邮件,也不拦截未验证用户访问,必须手动补全三处关键配置。
先确认你的 User 模型实现了 MustVerifyEmail 接口,并且数据库中 users 表存在 email_verified_at 字段(类型为 TIMESTAMP NULL):
php artisan make:migration add_email_verified_at_to_users_table --table=users,并在
up() 中写 $table->timestamp('email_verified_at')->nullable();
User 类需声明 implements MustVerifyEmail,并引入 use Illuminate\Contracts\Auth\MustVerifyEmail;
email_verified_at 字段会导致调用 $user->hasVerifiedEmail() 报错:“Undefined property: App\Models\User::$email_verified_at”Laravel 不会自动注册验证路由,必须显式调用 Auth::routes(['verify' => true]),否则访问 /email/verify 会 404,点击验证链接也会跳转失败:
routes/web.php 中,把 Auth::routes() 改为:Auth::routes(['verify' => true]);
GET /email/verify(提示页)、GET /email/verify/{id}/{hash}(验证入口)、POST /email/resend(重发入口)id 和 hash 参数,是 Laravel 签名 URL 机制,不可自行拼接或删减,否则 InvalidSignatureException 直接抛出仅实现接口和注册路由还不够——未验证用户仍能通过登录进入首页。必须用 verified 中间件拦截:
middleware(['auth', 'verified']),例如:Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth', 'verified']);auth 保证已登录,verified 才真正检查 email_verified_at IS NOT NULL;漏掉 auth 会导致未登录用户被重定向到 /email/verify,报错 “Trying to get property 'email_verified_at' of non-object”app/Http/Kernel.php 的 $middlewareGroups['web'] 里追加 \Illuminate\Auth\Middleware\EnsureEmailIsVe
rified::class,但更推荐按需加在路由层,避免影响登录页、密码重置等流程点击“重新发送验证邮件”按钮时,Laravel 默认调用 $user->sendEmailVerificationNotification(),它内部触发 EmailVerificationNotification 类;若你改过通知类或用了队列,这里容易出问题:
App\Notifications\VerifyEmail(或你自定义的类)继承 Illuminate\Notifications\Notification,且 toMail() 方法返回有效 Mailable
BROADCAST_DRIVER=redis 或 QUEUE_CONNECTION=database),重发请求会进队列;此时若队列未运行,用户点按钮无响应,也无错误提示 —— 建议开发期先关队列:QUEUE_CONNECTION=sync
App\Models\User::first()->sendEmailVerificationNotification();,观察日志或邮箱是否收到
最容易被忽略的是:验证链接有效期默认只有 24 小时,且签名绑定用户 ID 和邮箱值;一旦用户改邮箱,旧链接立即失效,但系统不会主动清理或提示——这点在多端登录或邮箱变更场景下要额外处理。