Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework Application Session Resource and Bootstrapping, what is wrong?

Hi: I am using the latest version of Zend Framework (1.9.3PL1). I set the following in my .ini

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

Next I want to initialize my session in my bootstrapper:

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

My question is, what goes in the initSession function? What should it return, if anything?

Furthermore, if i just start a session in there, it does not recognize the .ini configuration (e.g., the save_path is unchanged). However, if you move the start to a controller, the .ini configuration is recognized.

EDIT: A possible solution is:

protected function _initSession()
{
    // Based on http://framework.zend.com/issues/browse/ZF-6651
    $session = $this->getPluginResource('session'); 
    $session->init(); 
    Zend_Session::start();
}
like image 644
Jason Avatar asked Sep 25 '09 13:09

Jason


2 Answers

If you use the resources.session.*-options in your application configuration you must not have a _initSession() method in your bootstrap as these method will override the execution of the plugin resource session (Zend_Application_Resource_Session). The sole exitance of the resources.session.*-options in the configuration file will make sure that the session will be initialized according to your options.

Please read Zend_Application, Theory of Operation for a detailed discussion about the so-called resource methods and the resource plugins.

like image 93
Stefan Gehrig Avatar answered Jan 25 '23 12:01

Stefan Gehrig


Stefan is quite right, you are overriding the default session resource which makes use of those application options.

If you want to define your own _initSession() method and still access those options use something like:

protected function _initSession() 
{
    $options = $this->getOptions();
    $sessionOptions = array(
        'save_path' => $options['resources']['session']['save_path']
    );    
    Zend_Session::setOptions($options);
    Zend_Session::start();
}
like image 32
simonrjones Avatar answered Jan 25 '23 12:01

simonrjones