且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使会话过期重定向回登录?

更新时间:2023-09-28 23:27:40

如果您希望在对应用程序的每个HTTP请求期间运行中间件,只需在app/Http/Kernel的$ middleware属性中列出中间件类即可.php类. 因此,要保护每条路由免遭未经身份验证的访问,请这样做

If you want a middleware to be run during every HTTP request to your application, simply list the middleware class in the $middleware property of your app/Http/Kernel.php class. So, to protect every route from being accessed without authentication do this

protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        'App\Http\Middleware\Authenticate',// add this line according to your namespace
    ];

如果未登录,它将重定向用户.更新请记住,将auth中间件添加为全局文件将创建重定向循环,因此请避免使用它.

或者,如果您希望保护特定的路由,则将中间件auth附加到该路由

it will redirect the user if not logged in. UPDATE Keep in mind that adding auth middleware as global will create redirect loop so avoid it.

Or if you want specific routes to be protected then attach the middleware auth to that route

Route::get('admin/profile', ['middleware' => 'auth', function () {
    //
}]);

我认为您没有将auth中间件附加到您的路由上.

I think you are not attaching the auth middleware to your routes.