ZF2 SessionManager usage

Viewed 6133

I am new to ZF2 and not quite used to how to do stuff. I want to use session to keep track of an user (remember me). I had this code in one part of my class:

$sessionManager = new \Zend\Session\SessionManager();
$sessionManager->rememberMe($time);

// i want to keep track of my user id too
$populateStorage = array('user_id' => $user->getId());
$storage = new ArrayStorage($populateStorage);
$sessionManager->setStorage($storage);

Ok, so far so good. When i try:

var_dump($sessionManager->getStorage());

I get the expected data.

In another part of my program, i want to retreive my data again (a bit like containers):

$sessionManager = new \Zend\Session\SessionManager();
var_dump($sessionManager->getStorage());

This only returns an empty object. I guess this is due to the "new" and i think i have to implement SessionManager in a different way, but how? I do not know. This is what i came up with:

In my Module i now have:

public function onBootstrap(\Zend\Mvc\MvcEvent $e)
{
    $config = $e->getApplication()
            ->getServiceManager()
            ->get('Configuration');

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

In my module.config:

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

But how to procede? How do i get the instance for my sessionManager?

2 Answers
Related