我已经苦苦挣扎了很长一段时间,我的目标是将所有未登录用户访问的URL重定向到登录页面,但它给我返回了以下错误消息,我确定是因为在/login URL上创建了一个循环。身份验证也在登录页面检查授权用户。然而,我希望在检查授权时登录页面应该是一个例外情况。可能是我做错了什么,但我无法理解。以下是我的代码。
routes.php
身份验证类
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
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的新手,可能会做一些愚蠢的事情,为此道歉。谢谢您提前。