在laravel 5.5中进行会话时,如何自动重定向到登录页面

时间:2019-01-10 06:47:00

标签: php laravel session

我正在config/session.php中跟踪会话的配置

/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/

'lifetime' => env('SESSION_LIFETIME', 5),

'expire_on_close' => true,

我已使会话在用户闲置5分钟后失效,并重定向到登录名。它适用于所有路由并重定向用户以登录,但是会话过期后,当用户发送注销请求时,它会显示

 The page has expired due to inactivity.  Please refresh and try again. 

对于所有其他路线,它都可以正常工作。

该如何解决?

注意:我已经看到以下问题。没有一个对我有用。

Redirect automatically when user session expires in Laravel 5.5

Check for Session timeout in Laravel

1 个答案:

答案 0 :(得分:0)

您可以保护通往中间件的每条路径。

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
];


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 () {
//
}]);
相关问题