我现在可以在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中.
Symfony sessions store data like attributes in special ‘Bags’ which use a key in the $_SESSION superglobal. This means that a Symfony session cannot access arbitrary keys in $_SESSION that may be set by the legacy application,although all the $_SESSION contents will be saved when the session is saved. [From 07002]
我也看过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’放在正确的位置来处理所有这些键的迁移?
解决方法
解决方案是使用标量袋,就像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 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,1) === '_' && $key !== self::LEGACY_SESSION_BAG_NAME) { continue; } $bag->set($key,$value); } } }
正如下面的评论所解释的,这不一定是这样做的最佳方式.但它的确有效.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。