2014-01-13 120 views
0

我在做一個應用程序與Laravel 4.1,而當,我註冊了一些餅乾用戶登錄:曲奇隨機起至Laravel 4.1

Cookie::queue('adm-chave', $chave, 30); 
Cookie::queue('adm-usuario', $usuario->usuario, 30); 
Cookie::queue('adm-usuario-id', $usuario->id, 30); 
Cookie::queue('adm-usuario-nome', $usuario->nome, 30); 
Cookie::queue('adm-usuario-email', $usuario->email, 30); 

我做了一個代碼在更新過期時間每個頁面負載,就像如下:

Route::filter('logado', function() 
{ 
    $chave = Cookie::get('adm-chave'); 
    $usuario = Cookie::get('adm-usuario'); 
    $id_usuario = Cookie::get('adm-usuario-id'); 
    $nome = Cookie::get('adm-usuario-nome'); 
    $email = Cookie::get('adm-usuario-email'); 

    if(empty($chave) || empty($id_usuario)){ 
    $resposta = Redirect::to('/'); 
    $resposta->withCookie(Cookie::forget('adm-chave')); 
    $resposta->withCookie(Cookie::forget('adm-usuario')); 
    $resposta->withCookie(Cookie::forget('adm-usuario-id')); 
    $resposta->withCookie(Cookie::forget('adm-usuario-nome')); 
    $resposta->withCookie(Cookie::forget('adm-usuario-email')); 

    return $resposta; 
    } 

    Cookie::queue('adm-usuario', $usuario, 30); 
    Cookie::queue('adm-usuario-id', $id_usuario, 30); 
    Cookie::queue('adm-usuario-nome', $nome, 30); 
    Cookie::queue('adm-usuario-email', $email, 30); 
}); 

即使有這樣的更新,我的餅乾是隨機到期並被註銷了我的用戶,如果塊。

我錯過了什麼?

回答

0

首先,我們添加一個選項配置/ app.php爲以下(我已經將它設置爲24小時):

/* 
|-------------------------------------------------------------------------- 
| Session Files Lifetime 
|-------------------------------------------------------------------------- 
| 
| Here you may specify the number of minutes after which old sessions in 
| storage will be seen as 'garbage' and cleaned up. Garbage collection may 
| occur during session start, depending on the lottery. The default is 24. 
| 
*/ 

'files_lifetime' => 24*60, 

我們再創建兩個文件。在SessionServiceProvider.php我們覆蓋SessionManager註冊使用我們自己的SessionManager:

<?php namespace YourNamespace\Session; 

class SessionServiceProvider extends \Illuminate\Session\SessionServiceProvider { 

    /** 
    * Register the service provider. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     $this->setupDefaultDriver(); 

     $this->registerSessionManager(); 

     $this->registerSessionDriver(); 
    } 

    /** 
    * Register the session manager instance. 
    * 
    * @return void 
    */ 
    protected function registerSessionManager() 
    { 
     $this->app['session.manager'] = $this->app->share(function($app) 
     { 
      return new SessionManager($app); 
     }); 
    } 

} 

在SessionManager.php我們覆蓋getOptions()方法來設置gc_maxlifetime還有:

<?php namespace YourNamespace\Session; 

class SessionManager extends \Illuminate\Session\SessionManager { 

    /** 
    * Get the session options. 
    * 
    * @return array 
    */ 
    protected function getOptions() 
    { 
     $config = $this->app['config']['session']; 

     return array(
      'cookie_domain' => $config['domain'], 'cookie_lifetime' => $config['lifetime'] * 60, 
      'cookie_path' => $config['path'], 'cookie_httponly' => '1', 'name' => $config['cookie'], 
      'gc_divisor' => $config['lottery'][1], 'gc_probability' => $config['lottery'][0], 
      'gc_maxlifetime' => $config['files_lifetime'] * 60, 
     ); 
    } 

} 

這是解決方案是從https://github.com/laravel/framework/issues/2314

+0

試圖使用該方法,但沒有工作... 當我加載我的提供者數組上的兩個文件在一個pp.php,Laravel引發: ReflectionException 班級會話不存在 當我做php手工dump-auto時,它也會拋出: {「error」:{「type」:「ReflectionException」,「message」:「 Class會話不存在「,」file「:」\/home \/heitor \/public_html \/project \/vendor \/laravel \/framework \/src \/Illuminate \/Container \ /Container.php「,」行「:476}} 如何將這些文件包含到項目中,以及如何在app.php中調用它們? 我必須更改哪些文件以使Laravel知道並使用這些自定義提供程序? –

+1

這個解決方案是4.0,Session處理已經從4.1開始重建。 –