Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 get global session container

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! :-)

like image 737
Oliver Konig Avatar asked Dec 08 '13 17:12

Oliver Konig


2 Answers

Here's a more refined and improved version. It consists of the service "SessionService", a ViewHelper (which calls the SessionService), a ControllerPlugin (which also calls the SessionService), and shows how to set them up in the configuration file "module.config.php".

Make sure you set "use" paths or use absolute class paths in config.

SessionService.php:

class SessionService
{
    protected $sessionContainer;

    public function setSessionContainer(
        $sessionContainer
    ) {
        $this->sessionContainer = $sessionContainer;
    }

    public function __invoke() {
        return $this->sessionContainer;
    }
}

SessionHelper.php:

class SessionHelper extends \Zend\View\Helper\AbstractHelper
{
    protected $sessionService;

    public function setSessionService(
        $sessionService
    ) {
        $this->sessionService = $sessionService;
    }

    public function __invoke() {
        return $this->sessionService;
    }
}

SessionPlugin.php:

class SessionPlugin extends AbstractPlugin
{
    protected $sessionService;

    public function setSessionService(
        $sessionService
    ) {
        $this->sessionService = $sessionService;
    }

    public function __invoke() {
        return $this->sessionService;
    }
}

module.config.php:

'service_manager' => array(
    'factories' => array(
        'sessionService' => function(
            ServiceLocatorInterface $serviceLocator
        ) {
            $sessionContainer = new \Zend\Session\Container('base');
            $sessionService = new SessionService();
            $sessionService->setSessionContainer($sessionContainer);
            return $sessionService;
        },
    ),
),

'controller_plugins' => array(
    'factories' => array(
        'sessionPlugin' => function(
            AbstractPluginManager $pluginManager
        ) {
            $sessionService = $pluginManager->getServiceLocator()->get('sessionService');
            $sessionPlugin = new SessionPlugin();
            $sessionPlugin->setSessionService($sessionService);
            return $sessionPlugin;
        },
    ),
),

'view_helpers' => array(
    'factories' => array(
        'sessionHelper' => function (
            AbstractPluginManager $helperPluginManager
        ) {
            $sessionService = $helperPluginManager->getServiceLocator()->get('sessionService');
            $sessionHelper = new SessionHelper();
            $sessionHelper->setSessionService($sessionService);
            return $sessionHelper;
        },
    ),
),
like image 173
Oliver Konig Avatar answered Oct 27 '22 00:10

Oliver Konig


In your Controller write:-

use Zend\Session\Container;

  1. Make Session variable

                   $user_session = new Container('user');
    

'user' is Your Session Name To put Value in Your Session write:

                $user_session->username = 'xyz';
  1. After Storing You can Access Your Session By:

                               $user_session->  username
    
  2. To destroy Session Variable Use:

     $session = new Container('user');
     $session->getManager()->getStorage()->clear('user');
    

    it is Just Like : -

    unset($_SESSION['user']); http://wownewcode.blogspot.in/2013/12/set-session-in-zend-framework-2.html

like image 25
Dharmendra Avatar answered Oct 27 '22 01:10

Dharmendra