Zend framework Zend Framework应用程序会话资源和引导,有什么问题吗?

Zend framework Zend Framework应用程序会话资源和引导,有什么问题吗?,zend-framework,session,Zend Framework,Session,您好:我正在使用最新版本的Zend Framework(1.9.3PL1)。我在.ini中设置了以下内容 ; Bootstrap session resources resources.session.save_path = APPLICATION_PATH "/../data/sessions" resources.session.use_only_cookies = true resources.session.remember_me_seconds = 864000 接下来,我要在引导程

您好:我正在使用最新版本的Zend Framework(1.9.3PL1)。我在.ini中设置了以下内容

; 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_路径不变)。但是,如果将start移动到控制器,则会识别.ini配置

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

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

如果在应用程序配置中使用
resources.session.*
-选项,则在引导过程中不能有
\u initSession()
方法,因为这些方法将覆盖插件资源
会话的执行(
Zend\u application\u resource\u session
)。配置文件中
resources.session.*
-选项的唯一存在将确保会话将根据您的选项进行初始化


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

Stefan说得很对,您正在覆盖使用这些应用程序选项的默认会话资源

如果您想定义自己的_initSession()方法并仍然访问这些选项,请使用以下方法:

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

一些错误:必须是
$sessionOptions=array('save_path'=>$options['session']['save_path'])
Zend_Session::setOptions($sessionOptions)
    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();
}