【发布时间】: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