我正在尝试全局使用开箱即用的Authenticate
中间件,除了auth/login
和auth/logout
,因此我不需要在每个控制器中添加它。我将它添加到Kernel
中的全局中间件列表中(如下所示);然而,它陷入无限auth/login
重定向。对于任何访客,我希望将页面重定向到auth/login
并留在那里。
class Kernel extends HttpKernel
{
protected $middleware = [
...
\App\Http\Middleware\Authenticate::class,
];
}
这种情况正在发生,因为第一次点击auth/login
时,全局Authenticate
会一次又一次地重新定向到auth/login
。
是否可以像我所描述的那样全局使用默认的Authenticate
中间件?我需要为它创建一个新的中间件吗?
编辑:我的结论是托马斯的做法已经足够好了。
答案 0 :(得分:3)
您始终可以使用Route Groups。在routes.php
文件中......
// Your login/logout routes
Route::get('login', 'Auth\AuthController@getLogin');
Route::post('login', 'Auth\AuthController@postLogin');
Route::get('logout', 'Auth\AuthController@getLogout');
Route::group(['middleware' => 'auth'], function() {
// Put all other routes here and the auth middleware will be applied on them all
});
编辑:此外,您不需要将Authenticate
中间件添加到全局中间件堆栈。只需将其保留为默认$routeMiddleware
。
'auth' => \App\Http\Middleware\Authenticate::class,