Symfony 2 and custom session variables from legacy application

后端 未结 2 1354
梦谈多话
梦谈多话 2021-01-13 02:09

I\'m in the process of setting up the ability to migrate from a Legacy codebase to a Symfony one, and I\'m trying to share legacy session variables between the two applicati

2条回答
  •  一向
    一向 (楼主)
    2021-01-13 02:50

    Is there any complicated life-cycle for your session variables? Or they are mainly read-only once you're logged in into your application?

    If the latter applies, you might be able to add that "hack" into an event subscriber. This subscriber would listen for the Interactive Login Event, and it could look something like:

    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']);
            ...
        }
    }
    

    Hope this helps

提交回复
热议问题