Symfony 会话避免 _sf2_attributes [英] Symfony session avoid _sf2_attributes
问题描述
显然,我保存到 Symfony2 会话的每个键都在一个子键下:
Apparently every key I save to a Symfony2 session goes under a subkey:
$session->set('foo', 'bar');
将导致:
array('_sf2_attributes' => array('foo' => 'bar'));
这是一个问题,因为我正在构建一个与遗留代码交互的应用程序.我想使用 Symfony 的会话处理程序,但我希望应用程序的旧部分能够从 $_SESSION
读取.换句话说,我想要
This is a problem because I'm building an application that interacts with legacy code. I want to use Symfony's session handler but I want old parts of the app to be able to read from $_SESSION
. In other words, I want
$session->set('foo', 'bar');
和
$_SESSION['foo'] = 'bar';
具有相同的效果.
我没有看到任何配置选项来实现这一点.我使用我设置为
I didn't see any configuration option to achieve this. I use a specific session handler which I set to
session:
handler_id: my_app.session_handler
所以Symfony 使用不同的会话名称不会有问题.我能想到的唯一解决方案是围绕我的客户会话处理程序 (my_app.session_handler
) 创建一个新的会话处理程序类,以检查应用程序是否尝试访问 _sf2_attributes
但我认为它非常丑陋,我什至不确定是否可行.
So it won't be a problem that Symfony uses a different session name. The only solution I can think of is creating a new session handler class around my customer session handler (my_app.session_handler
) that checks if the application tries to access _sf2_attributes
but I think it's extremely ugly and I'm not even sure if would work.
谢谢!
推荐答案
这不仅是您必须自己实现的处理程序,还应该是您的 SessionStorage
.
This is not only the handler that you have to implement on your own, but it also should be your SessionStorage
.
默认情况下,Symfony2 使用 NativeSessionStorage
.您应该寻找方法 loadSession()
.如您所见,它使用从 getStorageKey()
接收到的 $key
初始化会话,该会话使用 _sf2_attributes
初始化 - 在 getStorageKey()
a href="https://github.com/symfony/HttpFoundation/blob/master/Session/Attribute/AttributeBag.php#L36-L39" rel="nofollow">AttributeBag
By default, Symfony2 uses NativeSessionStorage
. You should be looking for the method loadSession()
. As you can see, it initializes a session with the $key
received from getStorageKey()
, which is initialized with _sf2_attributes
here - in the AttributeBag
如果您在注入自定义会话存储时遇到问题,请告诉我.
Let me know if you have problems injecting custom session storage.
这篇关于Symfony 会话避免 _sf2_attributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!