2009-09-25 4 views
8

Hallo: Ich benutze die neueste Version von Zend Framework (1.9.3PL1). Ich habe folgendes in meinem iniZend Framework Application Session Ressource und Bootstrapping, was ist los?

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

nächsten mag ich meine Sitzung in meinem Bootstrap-Programm initialisieren:

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

Meine Frage ist, was geht in der initSession Funktion? Was sollte es zurückgeben, wenn überhaupt?

Darüber hinaus, wenn ich nur eine Sitzung dort starten, erkennt es nicht die .ini-Konfiguration (z. B. der save_path ist unverändert). Wenn Sie jedoch den Start zu einem Controller verschieben, wird die INI-Konfiguration erkannt.

EDIT: Eine mögliche Lösung ist:

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

Antwort

11

Wenn Sie die resources.session.* -Optionen in der Anwendungskonfiguration verwenden Sie keine _initSession() Methode in der Bootstrap, da diese Methode die Ausführung der außer Kraft setzen müssen Plugin-Ressource session (Zend_Application_Resource_Session). Die einzige Exitance der resources.session.*-Optionen in der Konfigurationsdatei wird sicherstellen, dass die Sitzung gemäß Ihren Optionen initialisiert wird.

Bitte lesen Sie Zend_Application, Theory of Operation für eine ausführliche Diskussion über die so genannten Ressource Methoden und die Ressource Plugins.

7

Stefan hat recht, Sie überschreiben die Standard-Sitzungsressource, die diese Anwendungsoptionen verwendet.

Wenn Sie Ihre eigene _initSession() Methode definieren und immer noch Zugriff auf diese Optionen verwenden, so etwas wie:

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

Einige Fehler: müssen 'sein $ sessionOptions = array ( 'save_path' => $ options ['session'] ['save_path'] ); 'und ' Zend_Session :: setOptions ($ sessionOptions); ' – Wizard

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