ZF2 get global session container

Viewed 11217

I found how to get a session container like this: $session = new \Zend\Session\Container('base');

But what if I need to access the session in many places during processing a HTTP request. Let's say in the Application module's indexAction in the IndexController, then I redirect it to the User\Controller\IndexController and need to access the session again, and then in a view helper or two, and who knows how often more.

When constructing the session container every time anew, that is a waste of processing time. Yes, I debugged it to see what's going on in the constructor, and yes, there is some code executed behind the scenes. It is not as if the constructor would just return a global variable or something else which would be immutable and doesn't need a construction process.

So what to do? Should I create a service for it? a controller plugin? a view helper? a service and a controller plugin and a view helper, with the latter calling the service?

I'm sure it is something that many people must have come across and have dealt with, but I can't find any information on this.

Any hint is dearly appreciated. Many thanks in advance! :-)

5 Answers
Related