2012-01-16 80 views
0

我正在使用codeigniter會話類(自動加載)。我如何設置初始化值?我需要這個,因爲我想初始化默認語言。如何初始化codeigniter會話數據?

最好的問候......

+0

http://codeigniter.com/user_guide/libraries /sessions.html – 2012-01-16 14:51:13

+0

我知道用戶指南。但是,如果我通過自動加載使用它,在哪裏設置會話的初始值?我不想在每個控制器中初始化它們! – fillibuster 2012-01-16 15:02:59

+0

啊,我以前不明白這個問題。我現在得到你想要的東西。你可以使用鉤子來做到這一點。請給我一點時間寫一個答案。 – 2012-01-16 15:16:56

回答

3

要加載的數據初始化到會話中,你可以使用hooks

您可以使用post_controller_constructor鉤子(在pre_controller鉤子中,會話可能尚未加載)。

打開了config/hooks.php和補充一點:

$hook['post_controller_constructor'][] = array(
           'class' => 'SessionData', 
           'function' => 'initializeData', 
           'filename' => 'SessionData.php', 
           'filepath' => 'hooks', 
           'params' => array() 
           ); 

然後在hooks文件夾製作一個名爲SessionData.php包含以下內容:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class SessionData{ 
    var $CI; 

    function __construct(){ 
     $this->CI =& get_instance(); 
    } 

    function initializeData() { 
      // This function will run after the constructor for the controller is ran 
      // Set any initial values here 
      if(!$this->session->userdata('language')){ 
      $this->CI->session->set_userdata('language', 'English'); 
      } 
    } 
} 
?> 
+0

很好,謝謝 – fillibuster 2012-01-16 15:40:04

+0

不客氣:-) – 2012-01-16 15:41:20