Could you tell me how to properly use sessions in ZF2? So far I have this code:
"session" =>
[
"remember_me_seconds" => 2419200,
"use_cookies" => true,
"cookie_httponly" => true
]
That's the session config I copied from some post here on stackoverflow. Now should I put this code into module.config.php in each module that uses sessions or in the Application module?
public function onBootstrap(EventInterface $Event)
{
$Config = $Event->getApplication()->getServiceManager()->get('Configuration');
$SessionConfig = new SessionConfig();
$SessionConfig->setOptions($Config['session']);
$SessionManager = new SessionManager($SessionConfig);
$SessionManager->start();
Container::setDefaultManager($SessionManager);
}
Same problem with the onBootstrap() method of the Module class. Should this code go into each module's Module class or just once into the Application's Module class?
In both cases I have tried both approaches and I even tried putting this code into both modules at once, but the only thing I was able to accomplish was to set session variables in controller's constructor and then read them in actions/methods. I wasn't able to set a session variable in one action/method and then read it in another. If I remove the lines in which I set the variables in controller's constructor, I can no longer see these variables in the session. The session just behaves like it was created and deleted each time a page is requested.
Am I missing something? Please don't link me to any resources on the internet, I have read them all and they're not really helpful.