代码之家  ›  专栏  ›  技术社区  ›  Sagar Gautam

如何在laravel5.5中会话退出时自动重定向到登录页面

  •  0
  • Sagar Gautam  · 技术社区  · 5 年前

    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 回复  |  直到 5 年前
        1
  •  0
  •   user10186369 user10186369    5 年前

    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 () {
    //
    }]);