2009-09-25 49 views
8

嗨:我正在使用最新版本的Zend Framework(1.9.3PL1)。我设置我的.iniZend Framework应用程序会话资源和引导,出了什么问题?

; Bootstrap session resources 
resources.session.save_path = APPLICATION_PATH "/../data/sessions" 
resources.session.use_only_cookies = true 
resources.session.remember_me_seconds = 864000 

下一页下面我想在我的引导程序初始化我会话:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initSession() 
    { 
     // What goes here!? 
    } 
} 

我的问题是,什么云在initSession功能?如果有的话,它应该返回什么?此外,如果我只是在那里启动一个会话,它不能识别.ini配置(例如,save_path不变)。但是,如果将开始移动到控制器,则会识别.ini配置。

编辑:一种可能的解决方案是:

protected function _initSession() 
{ 
    // Based on http://framework.zend.com/issues/browse/ZF-6651 
    $session = $this->getPluginResource('session'); 
    $session->init(); 
    Zend_Session::start(); 
} 

回答

11

如果您在使用应用程序配置的resources.session.* -options你不能在你的自举一个_initSession()方法,这些方法将覆盖的执行插件资源sessionZend_Application_Resource_Session)。 resources.session.* -options在配置文件中的唯一出口将确保会话将根据您的选项进行初始化。

请阅读Zend_Application, Theory of Operation有关所谓资源方法资源插件的详细讨论。

7

Stefan是非常正确的,您正在重写使用这些应用程序选项的默认会话资源。

如果要定义自己的_initSession()方法,并仍然可以访问那些选项类似:

protected function _initSession() 
{ 
    $options = $this->getOptions(); 
    $sessionOptions = array(
     'save_path' => $options['resources']['session']['save_path'] 
    );  
    Zend_Session::setOptions($options); 
    Zend_Session::start(); 
} 
+0

一些错误:必须是'$ sessionOptions =阵列( '的save_path'=> $ options ['session'] ['save_path'] );'和 'Zend_Session :: setOptions($ sessionOptions);' – Wizard 2014-01-27 08:00:46

3
protected function _initSession() 
{ 
    $config = array(); 
    $config['db'] = array('adapter'=>'PDO_SQLITE', 
        'params' => array('dbname'=> ROOT.'/data/tmp.db3') 

        ); 
    $config['SaveHandler'] = array(
     'name' => 'sessions', //table name as per Zend_Db_Table 
     'primary' => array(
      'id', //the sessionID given by PHP 
      'path', //session.save_path 
      'name', //session name 
     ), 
     'primaryAssignment' => array(
      //you must tell the save handler which columns you 
      //are using as the primary key. ORDER IS IMPORTANT 
      'sessionId', //first column of the primary key is of the sessionID 
      'sessionSavePath', //second column of the primary key is the save path 
      'sessionName', //third column of the primary key is the session name 
     ), 
     'modifiedColumn' => 'modified', //time the session should expire 
     'dataColumn'  => 'data',  //serialized data 
     'lifetimeColumn' => 'lifetime', //end of life for a specific record 
    ); 

    $config['lifetime'] = 60*60*24*30; 

    $config['options'] = array (
          'bug_compat_42' => '', 
          'bug_compat_warn' => '', 
          'cache_expire' => '180', 
          'cache_limiter' => 'nocache', 
          'cookie_domain' => '', 
          'cookie_httponly' => '', 
          'cookie_lifetime' => $config['lifetime'], 
          'cookie_path' => '/', 
          'cookie_secure' => '0', 
          'entropy_file' => '', 
          'entropy_length' => '0', 
          'gc_divisor' => '1000', 
          'gc_maxlifetime' => '1440', 
          'gc_probability' => '1', 
          'hash_bits_per_character' => '5', 
          'hash_function' => '0', 
          'name' => 'TaMeR_SESSID', 
          'referer_check' => '', 
          'save_handler' => 'user', 
          'save_path' => '', 
          'serialize_handler' => 'php', 
          'use_cookies' => '1', 
          'use_only_cookies' => 'on', 
          'use_trans_sid' => '0', 
          'strict' => false, 
          'remember_me_seconds' => $config['lifetime'], 
          'throw_startup_exceptions' => true, 
    ); 

    $db = Zend_Db::factory($config['db']['adapter'], $config['db']['params']); 
    if(! in_array('sessions', $db->listTables())) { 
     $sql = "CREATE TABLE sessions ("; 
     $sql .=  "id TEXT, "; 
     $sql .=  "path TEXT, "; 
     $sql .=  "name TEXT DEFAULT '', "; 
     $sql .=  "modified INTEGER, "; 
     $sql .=  "lifetime INTEGER, "; 
     $sql .=  "data TEXT, "; 
     $sql .=  "PRIMARY KEY (id, path, name)"; 
     $sql .= ");"; 
     $db->exec($sql); 
    } 
    Zend_Db_Table_Abstract::setDefaultAdapter($db); 
    Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config['SaveHandler'])); 
    Zend_Session::setOptions($config['options']); 
    Zend_Session::start(); 
} 
相关问题