且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Magento中使用Session

更新时间:2023-12-03 23:14:34

Magento的代码被组织成模块.模块的目的之一是提供名称空间.也就是说,模块允许一组开发人员编写代码,而不必担心它们的变量,对象等会被另一组开发人员意外踩踏.

Magento's code is organized into modules. One of the purposes of a module is to provide namespaces. That is, modules allow one group of developers to write code without fear that their variables, objects, etc. will be accidentally stomped on by another group of developers.

Magento中的每个模块都可以拥有自己的会话对象.通过提供每个模块自己的会话对象,Magento帮助开发人员避免了PHP全局会话变量中的名称冲突.例如,以下代码

Every module in Magento can have it's own session object. By giving each module it's own session object Magento helps developers avoid name conflicts in the PHP global session variable. For example, the following code

Mage::getModel('core/session')->setData('foo',$someValue);
Mage::getModel('customer/session')->setData('foo',$someOtherValue);

会将两个值保存到会话中,即使它们具有相同的密钥.

will save both values to the session, even though they have the same key.

关于应该选择哪个会话类-如果要编写自己的模块,则应创建自己的会话类/模型,从而避免上述冲突.

As to which session class you should choose — if you're writing your own module you should create your own session class/model, thereby avoiding the above mentioned conflicts.

但是实际上,只要您以某种方式命名变量,将内容保存在核心/会话上就不成问题.

Practically speaking though, saving things on core/session shouldn't be a problem so long as you namespace your variables in some way.

Mage::getModel('core/session')->setData('my_namespace_foo',$someValue);