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

Laravel 8 Fortify/Jetstream注册时邮箱验证后跳转至Intended URL问询

我来给你捋捋怎么实现这个需求——用户注册后验证邮箱,自动跳转到之前想访问的页面对吧?在Laravel 8 + Fortify + Jetstream的组合里,咱们可以通过几个关键步骤搞定:

步骤1:保存用户原本的目标URL

用户访问需要登录的页面时,Laravel会自动把这个URL存在session('url.intended')里,但注册流程可能会清空这个会话值。咱们可以通过事件监听把它存起来,防止丢失:

  1. 先创建一个事件监听器:
php artisan make:listener SaveIntendedUrlOnRegistration
  1. 编辑生成的监听器文件app/Listeners/SaveIntendedUrlOnRegistration.php:
<?php

namespace App\Listeners;

use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Session;

class SaveIntendedUrlOnRegistration
{
    public function handle(Registered $event)
    {
        // 把用户原本想访问的URL存到独立的会话键里
        if ($intendedUrl = Session::get('url.intended')) {
            Session::put('verify_intended_url', $intendedUrl);
        }
    }
}
  1. 在app/Providers/EventServiceProvider.php里注册这个监听器:
protected $listen = [
    // 保留其他已有的事件监听,新增下面这行
    \Illuminate\Auth\Events\Registered::class => [
        \App\Listeners\SaveIntendedUrlOnRegistration::class,
    ],
];
步骤2:自定义邮箱验证通知,带上目标URL参数

默认的验证邮件链接不会携带目标URL,咱们要自定义通知类,把目标URL作为参数拼到验证链接里:

  1. 生成自定义验证通知:
php artisan make:notification CustomVerifyEmail
  1. 编辑app/Notifications/CustomVerifyEmail.php:
<?php

namespace App\Notifications;

use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailNotification;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Session;

class CustomVerifyEmail extends VerifyEmailNotification
{
    protected function verificationUrl($notifiable)
    {
        // 取出之前保存的目标URL,默认跳转到仪表盘
        $intendedUrl = Session::get('verify_intended_url', route('dashboard'));
        
        // 生成带签名的临时验证链接,附加目标URL参数
        return URL::temporarySignedRoute(
            'verification.verify',
            now()->addMinutes(60),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
                'intended' => urlencode($intendedUrl)
            ]
        );
    }
}
  1. 在用户模型app/Models/User.php里,替换默认的验证通知:
<?php

namespace App\Models;

use App\Notifications\CustomVerifyEmail;
// 保留其他原有use语句

class User extends Authenticatable
{
    // 保留其他原有代码

    /**
     * 发送自定义邮箱验证通知
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new CustomVerifyEmail);
    }
}
步骤3:修改验证后的跳转逻辑

现在需要让验证邮箱成功后,自动跳转到我们传递的目标URL,咱们可以通过Fortify的服务提供者来修改默认逻辑:

编辑app/Providers/FortifyServiceProvider.php的boot方法:

use Laravel\Fortify\Http\Controllers\VerifyEmailController;

public function boot()
{
    // 保留其他原有代码
    
    // 自定义邮箱验证成功后的跳转行为
    $this->app->resolving(VerifyEmailController::class, function ($controller) {
        $controller->redirectTo = function ($request) {
            // 解码目标URL,默认跳转到仪表盘
            $intendedUrl = urldecode($request->query('intended', route('dashboard')));
            
            // 清理会话里的临时存储
            $request->session()->forget('verify_intended_url');
            
            // 跳转到用户原本想访问的页面
            return $intendedUrl;
        };
    });
}
测试流程
  1. 用户访问需要登录的页面(比如/dashboard),被重定向到注册页,此时会话里已经保存了目标URL
  2. 用户完成注册,收到带目标URL参数的验证邮件
  3. 用户点击验证链接,邮箱验证成功后自动跳转到/dashboard

如果用户注册前没有访问需要登录的页面,或者验证链接过期,就会默认跳转到仪表盘页面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 19:12:37