Symfony 2 and user session variables from an inherited application

I am trying to configure migration from Legacy code to Symfony, and I am trying to use legacy session variables between two applications.

Now I can var_dump($_SESSION)in app_dev.php, and the key user_idis from an outdated application. However, when I run the following in the controller, I do not get any session variables:

var_dump($request->getSession()->all());

To clarify , I currently have access to $_SESSION['user_id'], so the session is distributed among applications successfully, but the Symfony session object and parameter packages do not contain expired keys.

My goal:

  • For the key user_idthat will be available in the call$request->getSession()
  • For all other keys that will be available here.

My problems:

PHP Session bridge, , , . Symfony, , , - , Symfony.

Symfony , , "Bags", $_SESSION. , Symfony $_SESSION, , $_SESSION . [ ]

TheodoEvolutionSessionBundle, , Symfony 3.

, Symfony $_SESSION['_sf2_attributes'], , app_dev.php:

$_SESSION['_sf2_attributes']['user_id'] = $_SESSION['user_id'];

, session_start().

$_SESSION Symfony Session? - Symfony, ? , $_SESSION['_sf2_attributes'] "" , ?

+4
2

Symfony , . , .

, TheodoEvolutionSessionBundle. , - , ( symfony1 codeigniter ). , .

kernel.request, Symfony one:

if (isset($_SESSION['user_id'])) {
    $event->getRequest()->getSession()->set('user_id', $_SESSION['user_id']);
}

Jim edit

kernel.request - , , vars $_SESSION Symfony. :

namespace AppBundle\Session;

use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag,
    Symfony\Component\EventDispatcher\EventSubscriberInterface,
    Symfony\Component\HttpKernel\Event\GetResponseEvent,
    Symfony\Component\HttpKernel\KernelEvents;

class LegacySessionHandler implements EventSubscriberInterface
{
    /**
     * @var string The name of the bag name containing all the brunel values
     */
    const LEGACY_SESSION_BAG_NAME = 'old_app';

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => 'onKernelRequest'
        ];
    }

    /**
     * Transfer all the legacy session variables into a session bag, so $_SESSION['user_id'] will be accessible
     * via $session->getBag('old_app')->get('user_id'). The first bag name is defined as the class constant above
     *
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        /** There might not be a session, in the case of the profiler / wdt (_profiler, _wdt) **/
        if (!isset($_SESSION))
        {
            return;
        }

        $session = $event->getRequest()->getSession();

        /** Only create the old_app bag if it doesn't already exist **/
        try
        {
            $bag = $session->getBag(self::LEGACY_SESSION_BAG_NAME);
        }
        catch (\InvalidArgumentException $e)
        {
            $bag = new NamespacedAttributeBag(self::LEGACY_SESSION_BAG_NAME);
            $bag->setName(self::LEGACY_SESSION_BAG_NAME);
            $session->registerBag($bag);
        }

        foreach ($_SESSION as $key => $value)
        {
            /** Symfony prefixes default session vars with an underscore thankfully, so ignore these **/
            if (substr($key, 0, 1) === '_' && $key !== self::LEGACY_SESSION_BAG_NAME)
            {
                continue;
            }

            $bag->set($key, $value);
        }
    }
}

, . .

+2

- ? , ?

, , "" . " " , :

class PortSessionHandler implements EventSubscriberInterface
{
    private $session;

    public function __construct(SessionInterface $session) {
        $this->session = $session;
    }

    public static function getSubscribedEvents()
    {
        return [
            SecurityEvents::INTERACTIVE_LOGIN => [
                ['portLegacySession']
            ]
        ];
    }

    public function portLegacySession(InteractiveLoginEvent $event)
    {
        //here you could populate SF2 session with legacy session
        $this->session->set('user_id', $_SESSION['user_id']);
        ...
    }
}

,

+1

All Articles