Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Zend2 session in Symfony3 application

I have two applications. One the legacy one is written in Zend Framework and a new one in Symfony 3.1 should share session with an old one.

In old application native file storage is used so when I go to app_dev.php and I write session_start(); var_dump($_SESSION); I see '__ZF' key in the session and I need to get access to it in symfony3 application.

Obviously code above was only to check if session is shared within domain.

In symfony3 app I've tried to subcribe the event KernelEvents::REQUEST and there I wanted get raw session from the request and create a bag with 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 don't have access to raw session data from $event->getRequest();

This is how my bag looks so far. From what I understand I should have access to raw session data in 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 bag named "zf" I will have access to the data.

This is also my config 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 appreciated.

like image 317
Robert Avatar asked Jun 23 '16 12:06

Robert


1 Answers

I've managed to make it work.

Firstly I've changed configuration:

I've changed save path and remove native handler:

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

Then I've 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've imported class from Zend Zend\Stdlib\ArrayObject after that I have access to variable I want in bag zf.

like image 115
Robert Avatar answered Nov 15 '22 03:11

Robert