在Laravel 8中的Auth电子邮件验证后,如何更改重定向?

发布于 2025-01-19 17:07:40 字数 1426 浏览 2 评论 0原文

通过电子邮件验证成功注册后,我有 2 个条件。

  1. 如果新用户从主页选择计划,则重定向到注册页面并提交表单。然后将获得电子邮件验证链接,电子邮件验证后我想直接重定向到结帐。计划 ID 将保存会话,这样我就可以获得计划的所有详细信息。
  2. 如果新用户没有从主页选择计划,那么他可以注册并重定向到仪表板,

但在 Laravel 中,电子邮件验证后总是重定向到主页。但我不想再次重定向到主页。

这怎么能做到呢?哪里可以做编码部分?

验证控制器


 use VerifiesEmails;

    /**
     * Where to redirect users after verification.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }
    
    protected function verified(Request $request)
    {
        $request->session()->flash('alert','Your Email is verfied');
    }


路由

  public function emailVerification()
    {
        return function () {
            $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
            $this->get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
            $this->post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
        };
    }

I have 2 condition after successful registration with email verification.

  1. If the new user is select plan from home page, redirects to registration page submits the form. then will get Email verfication link, and after email verified I want to redirect directly to checkout. Plan id will be saving session , so I can get all the details of plan.
  2. If the new user is not select plan from home page, then he can sign up and redirects to dashboard

But in laravel after email verfication always redirects to home page. But I dont want to redirect to home page again.

How can be this done? Wher can do the coding part?

Verification Controller


 use VerifiesEmails;

    /**
     * Where to redirect users after verification.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }
    
    protected function verified(Request $request)
    {
        $request->session()->flash('alert','Your Email is verfied');
    }


Routes

  public function emailVerification()
    {
        return function () {
            $this->get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
            $this->get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
            $this->post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
        };
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰火雁神 2025-01-26 17:07:41

在Laravel 9*中对我有用的最佳选择是在房屋下方或之上定义一个新的公共常数
例如
位置:App \ Provers \ RouteserviceProvider

 public const SUB = '/account/subscription';

,然后转到verification controller
位置:app \ http \ controlter \ auth \ verification controller

,然后更改

  protected $redirectTo = RouteServiceProvider::HOME;

  protected $redirectTo = RouteServiceProvider::SUB;

The best option that worked for me in Laravel 9* was to define a new public constant in the RouteServiceProvider right under or above the HOME
e.g
Location: app\Providers\RouteServiceProvider

 public const SUB = '/account/subscription';

Then go to VerificationController
Location: app\Http\Controllers\Auth\VerificationController

and change

  protected $redirectTo = RouteServiceProvider::HOME;

to

  protected $redirectTo = RouteServiceProvider::SUB;
疯狂的代价 2025-01-26 17:07:41

添加一个称为redirectto()的方法。如果存在,它将被调用。

public function redirectTo()
{
   // put your routing logic here
}

该功能应返回一个URL的字符串。

Add a method called redirectTo(). It will be called if it exists.

public function redirectTo()
{
   // put your routing logic here
}

The function should return a string of the url to go to.

九命猫 2025-01-26 17:07:41

如果您使用的是强化,我认为解决此问题的更laravel方法是创建一个响应处理程序。

Laravel将在验证电子邮件后生成验证>验证。因此,要修改该逻辑,您将创建一个类并制作单身人士。

<?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\VerifyEmailResponse as VerifyEmailResponseContract;

class VerifyEmailResponse implements VerifyEmailResponseContract
{
    public function toResponse($request)
    {
        // Your custom logic returning redirect(), \Illuminate\Routing\Redirector or \Illuminate\Http\RedirectResponse
    }
}

在您的fortifyserviceProvider类中:

use Laravel\Fortify\Contracts\VerifyEmailResponse as VerifyEmailResponseContract;



public function boot()
{
    // logic
    $this->app->singleton(VerifyEmailResponseContract::class, VerifyEmailResponse::class);
    // logic
}

If you are using Fortify, I think the more Laravel way to solve this, is to create a Response handler.

Laravel will, when an email is verified, generate a VerifyEmailResponse. So to modify that logic, you would create a class and make that the singleton.

<?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\VerifyEmailResponse as VerifyEmailResponseContract;

class VerifyEmailResponse implements VerifyEmailResponseContract
{
    public function toResponse($request)
    {
        // Your custom logic returning redirect(), \Illuminate\Routing\Redirector or \Illuminate\Http\RedirectResponse
    }
}

And in your FortifyServiceProvider class:

use Laravel\Fortify\Contracts\VerifyEmailResponse as VerifyEmailResponseContract;



public function boot()
{
    // logic
    $this->app->singleton(VerifyEmailResponseContract::class, VerifyEmailResponse::class);
    // logic
}
夜司空 2025-01-26 17:07:41

夫妇从我可以看到的...

  1. 在您的控制器动作中,您将它们路由到将它们路由到
    auth \ verification controller@verify执行检查以确保标志
    尚未设置,如果未设置标志,请将其重定向
    之后逻辑。因此,这是这样的路线...

    返回redirect() - &gt; route('route.name',[$ veriyouwanttosendalong]));

或到具有错误的静态页面。

return redirect()->route('page/name')->withErrors(['Some error here']);
  1. 在您的Laravel Auth Controller中,应该是受保护的$ redirectto ='/home';操作您可以将其更改为仪表板或登录后要发送的任何地方。

Couple approaches from what I can see...

  1. In your controller actions where you are routing them to
    Auth\VerificationController@verify do the check to make sure a flag
    isn't already set, set the flag if it isn't set, put your redirect
    after that logic. So it would be something like this for a route...

    return redirect()->route('route.name', [$veriYouWantToSendAlong]);

Or to a static page with errors.

return redirect()->route('page/name')->withErrors(['Some error here']);
  1. In your Laravel auth controller should be a protected $redirectTo = '/home'; action you can change that to dashboard or wherever you'd like to send them after log in.
倾城月光淡如水﹏ 2025-01-26 17:07:41

也许将其添加到 Routes/web.php 中。
并将 /dashboard 更改为您喜欢的任何内容:)。

Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
 
    return redirect('/dashboard');
})->middleware(['auth', 'signed'])->name('verification.verify');

Maybe add this in the Routes/web.php.
And change the /dashboard to whatever you like :).

Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
 
    return redirect('/dashboard');
})->middleware(['auth', 'signed'])->name('verification.verify');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文