php – Symfony 2和遗留应用程序中的自定义会话变量
我正在设置从Legacy代码库迁移到Symfony代码的能力,我正在尝试在两个应用程序之间共享遗留会话变量.
我现在可以在app_dev.php中使用var_dump($_ SESSION),并且user_id密钥来自遗留应用程序.但是,当我在控制器中运行以下内容时,我没有得到任何会话变量: var_dump($request->getSession()->all()); 因此,为了澄清,我目前可以访问$_SESSION [‘user_id’],因此会话在应用程序之间成功共享,但Symfony会话对象及其参数包不包含旧密钥. 我的目标: >使user_id键在$request-> getSession()调用中可用 我的问题: 我已经尝试了PHP Session bridge,但我不认为这应该做我想要的.添加bridge参数不仅在我的Symfony应用程序中没有做任何事情,而且从我的read开始,这意味着在其他遗留应用程序中以某种方式使用,而不是在Symfony中.
我也看过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’]’hack’放在正确的位置来处理所有这些键的迁移? 解决方法
Symfony在会话中引入了会话包的概念,因此所有内容都是命名空间.访问非命名空间的会话属性没有构建解决方案.
解决方案是使用标量袋,就像TheodoEvolutionSessionBundle一样.我不会直接使用它,而是实现一些适用于你项目的自定义项目(它们只提供symfony1和codeigniter的集成).基于他们的想法,但根据您的需求进行调整. 或者,您可以实现一个kernel.request侦听器,它将遗留的会话属性重写为Symfony: if (isset($_SESSION['user_id'])) { $event->getRequest()->getSession()->set('user_id',$_SESSION['user_id']); } 吉姆的编辑 我在kernel.request上创建了一个事件监听器 – 每个请求进入,我们遍历$_SESSION中的所有遗留会话变量并将它们放在Symfony的会话包中.这是听众: namespace AppBundleSession; use SymfonyComponentHttpFoundationSessionAttributeNamespacedAttributeBag,SymfonyComponentEventDispatcherEventSubscriberInterface,SymfonyComponentHttpKernelEventGetResponseEvent,SymfonyComponentHttpKernelKernelEvents; 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,1) === '_' && $key !== self::LEGACY_SESSION_BAG_NAME) { continue; } $bag->set($key,$value); } } } 正如下面的评论所解释的,这不一定是这样做的最佳方式.但它的确有效. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |