You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 10中自定义VerifyEmail验证邮件URL

自定义Laravel验证邮件跳转URL至前端域名

在Laravel中,要替换验证邮件里的API域名为前端域名,可通过VerifyEmail类的createUrlUsing方法自定义URL生成逻辑,具体实现如下:

完整代码示例

// app/Providers/AuthServiceProvider.php

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Carbon;

class AuthServiceProvider extends ServiceProvider
{
    // ... 其他代码

    public function boot(): void
    {
        VerifyEmail::createUrlUsing(function ($notifiable) {
            // 生成验证所需的核心参数
            $userId = $notifiable->getKey();
            $hash = sha1($notifiable->getEmailForVerification());
            $expires = Carbon::now()->addMinutes(60)->getTimestamp();

            // 生成签名,确保参数不被篡改
            $signature = URL::signature(
                route('verification.verify', [
                    'id' => $userId,
                    'hash' => $hash,
                    'expires' => $expires,
                ])
            );

            // 提取签名中的查询参数
            $query = parse_url($signature, PHP_URL_QUERY);

            // 构造前端跳转URL,替换为你的前端域名
            $frontendUrl = "https://your-frontend-domain.com/verify-email?{$query}";

            return $frontendUrl;
        });
    }
}

关键说明

  • 参数匹配原生逻辑:通过$notifiable实例获取用户ID和邮箱哈希,确保和Laravel原生验证的核心参数一致,避免验证失败。
  • 保持安全机制:使用URL::signature生成带签名的参数,防止恶意篡改,和框架原生的安全校验规则对齐。
  • 前端跳转逻辑:将签名后的验证参数拼接到前端域名的验证页面路径,用户点击邮件链接后直接跳转到前端。
  • 后端验证衔接:前端页面拿到参数后,需调用后端的verification.verify接口(可通过AJAX请求)完成最终的邮箱验证流程。

内容的提问来源于stack exchange,提问作者Will

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 20:13:09