[英]Laravel 5.4 redirect to login if unauthenticated
I have a custom login page for my laravel app. 我有一个用于laravel应用程序的自定义登录页面。 However, i have modified my Handler.php to first look like this 但是,我修改了我的Handler.php使其看起来像这样
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest('login');
}
and even tried like this 甚至这样尝试
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect('login');
}
However, once I get to login, i get this error 但是,一旦我登录,就会出现此错误
How can i fix this problem?. 我该如何解决这个问题?
This worked for me 这对我有用
in the web.php
在web.php
Route::get('login', 'Auth\LoginController@login');
Route::group(['middleware' => ['auth']], function() {
// Route::auth();
in the login controller 在登录控制器中
public function login(){
return view('login-register');
}
in the handler.php
在handler.php
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest('login');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.