How to Bootstrap Sessions in Zend Framework 2

14,076

If i understand you correctly, all you wanna do is have your session working properly in your modules? Assuming that's correct there are two single steps.

1) Create the config: module.config.php

return array(
    'session' => array(
        'remember_me_seconds' => 2419200,
        'use_cookies' => true,
        'cookie_httponly' => true,
    ),
);

2) Start your Session: Module.php

use Zend\Session\Config\SessionConfig;
use Zend\Session\SessionManager;
use Zend\Session\Container;
use Zend\EventManager\EventInterface;

public function onBootstrap(EventInterface $evm)
{
    $config = $evm->getApplication()
                  ->getServiceManager()
                  ->get('Configuration');

    $sessionConfig = new SessionConfig();
    $sessionConfig->setOptions($config['session']);
    $sessionManager = new SessionManager($sessionConfig);
    $sessionManager->start();

    /**
     * Optional: If you later want to use namespaces, you can already store the 
     * Manager in the shared (static) Container (=namespace) field
     */
    Container::setDefaultManager($sessionManager);
}

Find more options in the documentation of \Zend\Session\Config\SessionConfig

If you want to store cookies too, then please see this Question. Credits to Andreas Linden for his initial answer - I'm simply copy pasting his.

Share:
14,076
dragonmantank
Author by

dragonmantank

Love to program in PHP, starting to look into Python and C# as well.

Updated on July 15, 2022

Comments

  • dragonmantank
    dragonmantank almost 2 years

    What is the best way to go about getting sessions up and running in Zend Framework 2? I've tried setting session_start() in my index.php file but then that gets run before any autoloaders have been bootstrapped, leading to incomplete objects sitting in my sessions.

    In ZF1 you could have it initialize sessions by adding some options into the config, but I'm at a loss on how to do this in ZF2.

  • dragonmantank
    dragonmantank over 11 years
    Thanks, this worked. As a note for anyone else using this, I threw this in my Application module (which I just use for most of my main config) and made sure it loaded first.
  • Green
    Green over 11 years
    Could you please explain how to set ZF2 Session to be automatically destroyed every time I close the browser? The standard way is to set cookie_lifetime = 0. But with ZF2 it doesn't work. It doesn't destroys the session with browser close. Also if I set remember_me_seconds = 1 (zero throws an exception) it doesn't work either - all info safely exists in the session after I close the browser. How to set such an option in ZF2? Thank you.
  • markus
    markus over 11 years
    As I understand it, it's wrong to use the static setDefaultManager to pass the manager to the container. You should instantiate the container and inject the manager via constructor or use setManager.
  • Sam
    Sam over 11 years
    First: Nothing that works, is really 'wrong'. Second: Instead of downvoting and putting out a clever line (no offense here, i know it sounds like!) please just provide an alternative answer with code from which we can learn of ;)
  • markus
    markus over 11 years
    I do not agree with 'what works can't be wrong'. There is obviously a lot of stuff out there that is fundamentaly broken and wrong but kind of works. The call to the static setter is just not necessary, you can skip it and the session works all the same. You'll only need to set this field if you ever want to use a Container (=namespace). I agree that the downvote wasn't strictly necessary but I found the answer confusing at my past stage of knowledge aquisition and that's what votes are for. Anyway, I'd remove it now but of course I can't.
  • Sam
    Sam over 11 years
    I don't bother about the downvote, it's more the lack of actual explanation that was missing (to what i understood) ;) Thanks for clarifying within the answer ;)