How to read a Zend2 session in a Symfony3 application

I have two applications. One of the obsolete ones is written in the Zend Framework, and the new one in Symfony 3.1 should share the session with the old one.

The old application uses its own file storage, so when I go to app_dev.php and I write session_start(); var_dump($_SESSION); session_start(); var_dump($_SESSION); , I see "__ZF" in the session, and I need to access it in a symfony3 application.

Obviously, the code above was only for checking if the session is shared in the domain.

In the symfony3 application, I tried to describe the KernelEvents::REQUEST event and there I wanted to get a raw session from the request and create a package with the parameters that come from Zend2.

 class SessionSubscriber implements EventSubscriberInterface { public function onKernelRequest(GetResponseEvent $event) { if ($event->isMasterRequest() && true == $event->getRequest()->hasSession()) { var_dump($event->getRequest()); $event->getRequest()->getSession()->registerBag(new ZendSessionBag()); var_dump($event->getRequest()->getSession()->getBag('zf')); exit; return; } } public static function getSubscribedEvents() { return array( KernelEvents::REQUEST => (array('onKernelRequest', 127)) ); } } 

But I do not have access to raw session data from $event->getRequest();

This is what my bag looks like. As far as I understand, I should have access to the raw session data in the initialize () method

 class ZendSessionBag implements SessionBagInterface { private $storageKey; private $sessionData; public function __construct($storageKey = '__ZF') { $this->storageKey = $storageKey; } public function getName() { return 'zf'; } public function initialize(array &$array) { var_dump($array); // here should be raw $_SESSION data $this->sessionData = &$array; } public function getStorageKey() { return $this->storageKey; } public function clear() { $this->sessionData = []; } } 

So when I get the session and then get the bag named "zf", I will have access to the data.

This is also my configuration regarding sessions:

 session: storage_id: session.storage.php_bridge handler_id: session.handler.native_file save_path: "/var/lib/php5/sessions" 

Any help would be greatly appreciated.

+7
php symfony session zend-framework2
source share
1 answer

I managed to get it to work.

First, I changed the configuration:

I changed the save path and removed the handler:

 session: save_path: "/var/lib/php5/sessions" 

Then I changed EventSubscriber:

 class SessionSubscriber implements EventSubscriberInterface { /** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { $bag = null; $session = $event->getRequest()->getSession(); try { $bag = $session->getBag('zf'); } catch (\InvalidArgumentException $e) { $bag = new NamespacedAttributeBag("__ZF"); $bag->setName("zf"); $session->registerBag($bag); $session->start(); } $bag->set('userId', isset($_SESSION['Zend_Auth']->storage) ? $_SESSION['Zend_Auth']->storage : null); } /** * @return array */ public static function getSubscribedEvents() { return array( KernelEvents::REQUEST => (array('onKernelRequest', 127)) ); } } 

I imported the class from Zend Zend\Stdlib\ArrayObject after that I have access to the variable that I want in the zf bag.

+2
source share

All Articles