Laravel登录重定向您的次数太多
我一直在努力解决这个问题,从现在开始,我正在尝试将所有未登录用户点击的url重定向到登录页面,它给了我这个错误,我确信这是因为它在/login url上创建了一个循环。身份验证也在登录页面中检查授权用户。然而,我希望登录页面在检查身份验证时是一个例外。我可能做错了一些我无法得到的事情。这是我的密码 routes.phpLaravel登录重定向您的次数太多,laravel,laravel-5.1,laravel-5.2,laravel-routing,Laravel,Laravel 5.1,Laravel 5.2,Laravel Routing,我一直在努力解决这个问题,从现在开始,我正在尝试将所有未登录用户点击的url重定向到登录页面,它给了我这个错误,我确信这是因为它在/login url上创建了一个循环。身份验证也在登录页面中检查授权用户。然而,我希望登录页面在检查身份验证时是一个例外。我可能做错了一些我无法得到的事情。这是我的密码 routes.php Route::post('login', 'Auth\AuthController@login'); Route::get('login' , 'Auth\AuthControl
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
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,
];
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
});
Route::auth();
Route::group(['middleware' => 'auth'], function () {
// All route your need authenticated
});
kernel.php
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
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,
];
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
});
Route::auth();
Route::group(['middleware' => 'auth'], function () {
// All route your need authenticated
});
验证类
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);
}
}
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', '/']]);
}
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\webUser::class, //Remove
],
protected $routeMiddleware = [
'webUser'=> \App\Http\Middleware\webUser::class //Keepit
]
AuthController类
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);
}
}
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', '/']]);
}
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\webUser::class, //Remove
],
protected $routeMiddleware = [
'webUser'=> \App\Http\Middleware\webUser::class //Keepit
]
请帮帮我,这一切都超出了我的想象,对我来说似乎是某种火箭科学。顺便说一句,我是新来的拉威尔,可能正在做一些愚蠢的事情,为此道歉。提前感谢。为什么您这么做只是为了将所有未登录的用户重定向到登录表单 我想你可以这么做 Routes.php
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
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,
];
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
});
Route::auth();
Route::group(['middleware' => 'auth'], function () {
// All route your need authenticated
});
身份验证控制器构造应该是这样的
AuthController
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
为什么你这么做只是为了将每个未登录的用户重定向到登录表单 我想你可以这么做 Routes.php
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
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,
];
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
});
Route::auth();
Route::group(['middleware' => 'auth'], function () {
// All route your need authenticated
});
身份验证控制器构造应该是这样的
AuthController
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
问题在于你的路线
当我进入并没有注销时,您会将我发送到登录(get)路径。当您在AuthController的construct函数中指定中间件时,每次调用AuthController的方法时,都会再次调用construct函数,并在登录时将其发送回您。。它会无限期地重复。问题在于你的路线
当我进入并没有注销时,您会将我发送到登录(get)路径。当您在AuthController的construct函数中指定中间件时,每次调用AuthController的方法时,都会再次调用construct函数,并在登录时将其发送回您。。它会无限期地重复。您需要在Laravel组之外添加路由登录: routes.php
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
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,
];
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
});
Route::auth();
Route::group(['middleware' => 'auth'], function () {
// All route your need authenticated
});
另外,您可以使用以下方式查看您的路线列表:
php artisan route:list
您需要在Laravel组之外添加路由登录: routes.php
Route::post('login', 'Auth\AuthController@login');
Route::get('login' , 'Auth\AuthController@showLoginForm');
Route::get('/' , 'Auth\AuthController@showLoginForm');
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,
];
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
});
Route::auth();
Route::group(['middleware' => 'auth'], function () {
// All route your need authenticated
});
另外,您可以使用以下方式查看您的路线列表:
php artisan route:list
比如说@mkmnstr
问题在于你的路线。 当我进入并没有注销时,您会将我发送到登录(get)路径。当您在AuthController的construct函数中指定中间件时,每次调用AuthController的方法时,都会再次调用construct函数,并在登录时将其发送回您。。它会无限期地重复 为了解决这个问题,你应该添加
Auth::logout();
这里
比如说@mkmnstr
问题在于你的路线。 当我进入并没有注销时,您会将我发送到登录(get)路径。当您在AuthController的construct函数中指定中间件时,每次调用AuthController的方法时,都会再次调用construct函数,并在登录时将其发送回您。。它会无限期地重复 为了解决这个问题,你应该添加
Auth::logout();
这里
如果您使用自定义中间件,则必须遵守所有规则 在我的例子中,我必须在web中间件组中定义一个自定义路由类。 在复制粘贴的世界里,有时我们会犯错误 中间件:
public function handle($request, Closure $next)
{
if(!isset(session('user'))){
return redirect('login');
}
return $next($request);
}
}
我在Kernel.php中的错误
如果web$middlewareGroups中存在自定义中间件类,则它将检查条件2次,因此它将给出如下错误:重定向您的次数过多
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);
}
}
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', '/']]);
}
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\webUser::class, //Remove
],
protected $routeMiddleware = [
'webUser'=> \App\Http\Middleware\webUser::class //Keepit
]
如果您使用自定义中间件,则必须遵守所有规则 在我的例子中,我必须在web中间件组中定义一个自定义路由类。 在复制粘贴的世界里,有时我们会犯错误 中间件:
public function handle($request, Closure $next)
{
if(!isset(session('user'))){
return redirect('login');
}
return $next($request);
}
}
我在Kernel.php中的错误
如果web$middlewareGroups中存在自定义中间件类,则它将检查条件2次,因此它将给出如下错误:重定向您的次数过多
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);
}
}
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', '/']]);
}
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\webUser::class, //Remove
],
protected $routeMiddleware = [
'webUser'=> \App\Http\Middleware\webUser::class //Keepit
]
我知道我做错了什么?我知道我做错了什么?那么我该怎么做呢?我知道我做错了什么?谢谢你的回答,我会在一段时间内尝试这个,我知道我做的很糟糕,但是你能告诉我我在哪里创建了一个循环吗?我做了你提到的,它给了我同样的错误,它不能解决这个问题。我删除了我的路由并放置了你的路由,还替换了我的AuthController构造函数,但运气不好..在你的AuthController中更改这个,试试这个
$this->中间件('auth',['except'=>'showloginfo'])代码>我认为当在构造函数中使用中间件时,必须为expect或only指定方法名,因此不应该为Showlogin设置中间件,也不应该为Login设置中间件,但我不知道如何删除$this->middleware('guest');从我的ctrl类中,并将我的所有路线添加到您在路线中发送给我的组中。这一切都如我所料。好吧,否则我的代码中会有一些漏洞。不,好吧,我给你的小组做了与$this->middlware相同的事情,而是在类中定义一个中间件在routes中定义它。phpThanks谢谢你的回答,我会在某个时候尝试一下,我知道我做得很糟糕,但是你能告诉我我在哪里创建了一个循环吗?我做了你提到的事情,它给了我同样的错误,它不能解决问题。我删除了我的路由并放置了你的路由,还替换了我的AuthController构造函数,但运气不好..在你的AuthController中更改这个,试试这个$this->中间件('auth',['except'=>'showloginfo'])代码>我认为在构造函数中使用中间件时,必须为expect或only指定方法名,因此不应为Showlogin设置中间件,而不是Login(我让它工作:),但我不知道如何删除$thi