我现在已经安静了一段时间一直在努力解决这个问题,我正在尝试将未登录用户的所有网址重定向到登录页面,它给了我这个错误,我肯定是因为它创建了一个循环在/登录URL。身份验证也是在登录页面中检查授权用户。但是我希望在检查auth时登录页面应该是一个例外。我可能做错了,我无法得到。这是我的代码。
routes.php文件
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Auth\Access\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'acl' => \App\Http\Middleware\CheckPermission::class,
];
认证课程
class Authenticate
{
public function handle($request, Closure $next, $guard = null) {
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
}
AuthController类
class AuthController extends Controller {
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/dashboard';
protected $loginPath = '/login';
protected $redirectPath = '/dashboard';
public function __construct(){
$this->middleware('auth', ['except' =>'login']);
/* I have been trying these many things to fix this, all in loss.
// $this->middleware('acl'); // To all methods
// $this->middleware('acl', ['only' => ['create', 'update']]);
// $this->middleware('guest', ['only' => ['/login']]);
// echo "Message"; exit;
// $this->middleware('auth');
// $this->middleware('auth', ['only' => ['login']]);
// $this->middleware('auth', ['only' => ['/login']]);
// $this->middleware('auth', ['except' => 'login']);
// $this->middleware('guest');
// $this->middleware('guest', ['only' => ['logout' , 'login', '/login', '/']]);
}
请帮助我,它一切都在我头上,对我来说似乎是某种火箭科学。好顺便说一下,我是laravel的新手,可能会做一些愚蠢的事情,为此道歉。提前致谢。
为什么要这样做只是为了将每个未登录的用户重定向到登录表单?
我想你可以这样做
routes.php文件
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
Route::group(['middleware' => 'auth'], function () {
// any route here will only be accessible for logged in users
});
和auth控制器构造应该是这样的
AuthController
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
您需要在Laravel组外添加路由登录:
routes.php文件
Route::auth();
Route::group(['middleware' => 'auth'], function () {
// All route your need authenticated
});
此外,您可以使用以下方式查看您的路线列表:
php artisan route:list
问题出在您的路线上。
当我进入并且我没有退出时,你发送给我登录(获取)路线。当您在AuthController中的构造函数中指定中间件时,每次调用AuthController的方法时,都会再次调用构造函数并在登录时将其发回...并且它会无限重复。
比如@mkmnstr说
问题出在您的路线上。当我进入并且我没有退出时,你发送给我登录(获取)路线。当您在AuthController中的构造函数中指定中间件时,每次调用AuthController的方法时,都会再次调用构造函数并在登录时将其发回...并且它会无限重复。
解决你应该添加的问题
Auth::logout();
这里
...
} else {
Auth::logout(); // user must logout before redirect them
return redirect()->guest('login');
}
...