为什么 Symfony2 建议避免使用“遗留"的 php 会话? [英] Why does Symfony2 recommend avoiding use of 'legacy' php sessions?

查看:40
本文介绍了为什么 Symfony2 建议避免使用“遗留"的 php 会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用标准 PHP 构建的 Web 应用程序.我正在通过使用 Symfony2 构建子应用程序(针对站点的管理员/所有者)来学习 Symfony.到目前为止一切顺利..
我的 symfony 应用程序确实从父"应用程序调用了一些初始化代码,并且该初始化代码设置了该子应用程序可能想要或可能不想使用的一些(遗留)会话变量.

I have a web application built with standard PHP. I'm learning Symfony by building a sub-application (for administrators/owners of the site) using Symfony2. So far so good..
My symfony app does call some initialization code from the 'parent' application, and that initialization code sets some (legacy) session variables that this child application may or may not want to use.

但我在 Symfony 文档中注意到他们建议避免使用旧版 PHP 会话.http://symfony.com/doc/current/components/http_foundation/sessions.htmlhttp://symfony.com/doc/current/components/http_foundation/session_php_bridge.html

But I've noticed in the Symfony documentation that they recommend avoiding use of legacy PHP sessions. http://symfony.com/doc/current/components/http_foundation/sessions.html http://symfony.com/doc/current/components/http_foundation/session_php_bridge.html

他们为什么提出这个建议?

Why do they make this recommendation?

是否仅仅是因为 Symfony 会话管理更好",(并且使用旧的 SESSION 超全局变量在某种程度上是一种反模式)--- 或者,是否存在任何其他特定的不兼容或可能导致的问题我的父"应用程序中的代码正在使用旧会话这一事实?或者其他一些/额外的原因?

Is it simply because the Symfony session management is "better", (and use of the legacy SESSION superglobal is somewhat of an anti-pattern) --- or, is there any other specific incompatibility or problem that may be caused due to the fact that code from my 'parent' application is using legacy sessions? Or some other / additional reasons?

推荐答案

其实在 http://symfony.com/doc/current/components/http_foundation/session_php_bridge.html,但不是那么明显:

It's actually explained on http://symfony.com/doc/current/components/http_foundation/session_php_bridge.html, but it's not so obvious:

  1. SF2 也使用了 $_SESSION,但它封装"了 session 服务中的功能.您不必担心 session_start 和所有这些 - 只需正确配置它,然后通过服务访问它.

  1. SF2 also used the $_SESSION, but it "encapsules" the functionality in the session service. You don't have to worry about session_start and all that – just configure it properly, and then access it via the service.

文档提到了存储会话数据的包".这些包"是具有 SF 特定结构的数据容器.如果遗留服务完全控制 $_SESSION,它可能会损坏这些结构.另一方面,遗留服务可能会创建 SF2 不知道的结构,并且可能会损坏.

The documentation mentions "bags" wherein the session data is stored. These "bags" are data containers with a SF-specific structure. If a legacy service would take full control over the $_SESSION, it could damage these structures. On the other hand, legacy services might create structures which SF2 is not aware of, and might damage.

例如,这是 Symfony2 中 print_r(array_keys($_SESSION)); 的结果:

For example, this is the result of a print_r(array_keys($_SESSION)); in Symfony2:

Array
(
    [0] => _sf2_attributes
    [1] => _sf2_flashes
    [2] => _sf2_meta
)

总的来说,我不会说 SF 的会话处理好坏——作为一个框架,它只是为会话管理的常见问题提供了一个实现.

In general, I wouldn't say that SF's session handling is better or worse – as a framework, it simply provides an implementation for the common problem of session management.

最多可以认为它优于天真的"实现,尤其是(抱歉:)PHP 新手",他们不了解会话处理的所有含义.

At most, it could be considered superior to "naive" implementations, especially by (sorry:) "PHP newbies", who don't understand all the implications of session handling.

并且由于会话的性质(尤其是 PHP 中的 $_SESSION 超全局变量),您无法 100% 避免与遗留代码的冲突,这就是为什么他们指出并提出解决方案的原因处理这类问题.

And due to the nature of sessions (especially with the $_SESSION superglobal in PHP), you cannot 100% avoid collisions with legacy code, which is why they point it out and propose solutions how to handle this kind of problem.

这篇关于为什么 Symfony2 建议避免使用“遗留"的 php 会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆