【问题标题】:Change Redirect Path更改重定向路径
【发布时间】:2017-01-20 07:25:43
【问题描述】:

所以我在 laravel 框架(版本 5.3)中构建这个 web 应用程序,我想在用户登录失败时更改重定向路径,到目前为止,我已经在我的 LoginController.php 中尝试了这段代码

protected $loginPath = "/my-given-path";

但它似乎不起作用,所以我尝试检查这个 AuthenticatesUsers 特征并进入这个文件,然后进入 RedirectUsers 特征,并且不存在围绕 $loginPath 旋转的这样的函数,但 $redirectTo 存在。


好的,所以我回到 AuthenticatesUsers 特征并检查了几个函数以看到这一点:-

public function login(Request $request)
{
    $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->credentials($request);

    if ($this->guard()->attempt($credentials, $request->has('remember'))) {
        return $this->sendLoginResponse($request);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    if (! $lockedOut) {
        $this->incrementLoginAttempts($request);
    }

    return $this->sendFailedLoginResponse($request);
}

因此我需要检查这个 `sendFailedLoginResponse()' 函数,所以在这里:-

 protected function sendFailedLoginResponse(Request $request)
{
    return redirect()->back()
        ->withInput($request->only($this->username(), 'remember'))
        ->withErrors([
            $this->username() => Lang::get('auth.failed'),
        ]);
}

所以现在我尝试将redirect()->back()... 更改为redirect('/my-given-path')...,但仍然没有运气。


我一整天都在尝试解决这个问题,但无法提出解决方案,而且之前问的不是 laravel 5.3 版本。 因此,如果有人能告诉我发生了什么以及我哪里错了,我会很高兴?

【问题讨论】:

  • 我猜它是因为验证而不是因为身份验证失败而重定向回相同的网址
  • 哦,是的,我认为你是对的!那么如果验证失败,有什么方法可以处理重定向到的路径?
  • 将此函数添加到 loginController protected function getRedirectUrl() { return 'path'; }
  • 不,它仍然重定向回同一页面。
  • 把它们都改成你想要的路径

标签: php laravel redirect laravel-5.3


【解决方案1】:

好的,所以我找到了解决方案,实际上我认为我在函数sendFailedLoginResponse() 下的AuthenticatesUsers.php 文件中将redirect()->back()... 更改为redirect('/my-given-url')... 时做错了。


但是当 $redirectTo 存在时,$loginPath 不再工作,并且 RedirectUsers 特征中不存在这样的功能。 所以我必须在 AuthenticatesUsers trait 的函数中手动硬编码我想要的路径。

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 2015-07-31
    • 2023-03-28
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多