简体   繁体   English

Laravel 打算总是返回电子邮件/验证

[英]Laravel intended always returns email/verify

I'm using Laravel 7.4, and I want to redirect the users to the page they came from after the login.我正在使用 Laravel 7.4,我想将用户重定向到登录后他们来自的页面。 I edited the file RedirectIfAuthenticated and added the redirect like:我编辑了文件 RedirectIfAuthenticated 并添加了重定向,如:

return redirect()->intended(RouteServiceProvider::HOME);

But the problem is, it always redirected me to home.但问题是,它总是把我重定向到家。 When I actually logged what the intended function returns, it always returns the url as website.com/email/verify.当我实际记录预期函数返回的内容时,它总是将 url 作为 website.com/email/verify 返回。 I'm 100% sure I don't redirect the users to that url, because I am logging in with an already verified user.我 100% 确定我不会将用户重定向到该网址,因为我正在使用已验证的用户登录。

It might be some FW setup that I failed to notice, since I took over this project from another developer.这可能是我没有注意到的一些固件设置,因为我从另一个开发人员那里接手了这个项目。 I'll provide any more info if needed.如果需要,我会提供更多信息。

Check for middleware maybe you are redirecting unverified users to email/verify检查中间件也许您正在将未经验证的用户重定向到电子邮件/验证

Then in your LoginController create a protected function redirectTo() and return url()->previous();然后在您的LoginController中创建一个protected function redirectTo()并返回 url()->previous();

Your code will look like this您的代码将如下所示

    protected function redirectTo()
    {
        return url()->previous();
    }

Or Also you can add $this->redirectTo = url()->previous() to your constructor like this或者您也可以像这样将$this->redirectTo = url()->previous()添加到您的构造函数中

    public function __construct()
    {
        $this->redirectTo = url()->previous();
        $this->middleware('guest')->except('logout');
    }

Hope this helps希望这可以帮助

If you dump dd(session()->all()) of a so-called "intended" page, you will see the following:如果您转储所谓的“预期”页面的dd(session()->all()) ,您将看到以下内容:

在此处输入图像描述

As you can see, Laravel store user's previously intended page at url['intended'] array element.如您所见,Laravel 将用户先前想要的页面存储在url['intended']数组元素中。 So to imitate this behavior manually, you have to set this value manually where needed.所以要手动模仿这种行为,你必须在需要的地方手动设置这个值。 In this case it's LoginController :在这种情况下,它是LoginController

public function showLoginForm()
{
    session()->put('url.intended', url()->previous());
    return view('admin.auth.login');
}

Now the original location has been stored, and later when the user log in, they will be redirected as declared in RedirectIfAuthenticated@handle :现在原始位置已存储,稍后当用户登录时,它们将按照RedirectIfAuthenticated@handle中的声明进行重定向:

return redirect()->intended(RouteServiceProvider::HOME);

But what if the user went to the /login page intentionally, or simply put, what if they refresh the page?但是,如果用户有意访问/login页面会怎样,或者简单地说,如果他们刷新页面会怎样? url.intended will have the value of route('login') , then when user logged in, they will be redirected to login page and the infinite loop continues. url.intended将具有route('login')的值,然后当用户登录时,他们将被重定向到登录页面并继续无限循环。

In that case, url.intended should not have any value.在这种情况下, url.intended不应该有任何价值。 RouteServiceProvider::HOME value will be used as destination. RouteServiceProvider::HOME值将用作目的地。 There must be additional condition for that, let's say your site have login route named login :必须有附加条件,假设您的站点有名为login的登录路由:

public function showLoginForm()
{
    if (($url()->previous() != route('login')) {
        session()->put('url.intended', url()->previous());
    }        
    return view('admin.auth.login');
}

Case anything unclear, you may want to have a look at similar question如果有不清楚的地方,你可能想看看类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM