Php Laravel 5.3-登录尝试重定向失败
在我的应用程序中,我使用Laravel 5.3的默认身份验证。我有两个用户可以登录的地方,如果登录失败,我想重定向到这两个地方中的一个。换句话说,无论用户通过哪个视图登录,如果他们提交了无效的凭据,他们总是会被重定向回一个特定的视图,而不是他们从哪个视图提交表单。在Laravel5.1中,这似乎可以通过在登录控制器中包含变量来实现。事实上,他们似乎已经从文档中删除了这个选项,所以我不确定如何处理这个问题Php Laravel 5.3-登录尝试重定向失败,php,laravel,authentication,redirect,laravel-5.3,Php,Laravel,Authentication,Redirect,Laravel 5.3,在我的应用程序中,我使用Laravel 5.3的默认身份验证。我有两个用户可以登录的地方,如果登录失败,我想重定向到这两个地方中的一个。换句话说,无论用户通过哪个视图登录,如果他们提交了无效的凭据,他们总是会被重定向回一个特定的视图,而不是他们从哪个视图提交表单。在Laravel5.1中,这似乎可以通过在登录控制器中包含变量来实现。事实上,他们似乎已经从文档中删除了这个选项,所以我不确定如何处理这个问题 非常感谢您的任何想法和/或建议。谢谢 编辑:我误解了你原来的问题。这已更新。 如果需要对此进
非常感谢您的任何想法和/或建议。谢谢 编辑:我误解了你原来的问题。这已更新。 如果需要对此进行自定义,可以执行以下操作: 打开
App\Http\Controllers\Auth\LoginController
(假设您使用的是php artian make:Auth
命令),并添加以下内容:
/**
* Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendFailedLoginResponse(Request $request)
{
return redirect()->to('/the_redirect_location')
->withInput($request->only($this->username(), 'remember'))
->withErrors([
$this->username() => Lang::get('auth.failed'),
]);
}
这将覆盖LoginController
使用的\illighted\Foundation\Auth\AuthenticatesUsers
特征中包含的相同方法。redirect()->to('/The\u redirect\u location')
是我更改的部分。最初是:redirect()->back()
如果选择使用此方法,请确保将其添加到LoginController
的顶部:
use Lang;
use Illuminate\Http\Request;
如果有人正在寻找Laravel版本
5.7
的解决方案。我通过将以下sendFailedLoginResponse
方法添加到LoginController
/**
* Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function sendFailedLoginResponse()
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.failed')],
])
->redirectTo("/the_redirect_location");
}
不要忘记以下导入内容:
use Illuminate\Validation\ValidationException;
注意:此方法与它从illighted\Foundation\Auth\AuthenticateUsers
覆盖的方法之间的区别在于行->redirectTo(“
/_重定向_位置”)代码>
其中,/u redirect\u location
是身份验证失败时要重定向到的URL。打开文件Authenticate.php,对每个失败的路由更改方法redirectTo()
protected function redirectTo($request) {
if (! $request->expectsJson()) {
if ($request->path() == "user/dashboard"){
return route('user.login.form');
} else {
return route('HOME');
}
}
}