如何在Magento中使用Session [英] How to use Session in Magento

查看:226
本文介绍了如何在Magento中使用Session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察到Magento中有多个会话类,例如,Mage :: getModel('core/session'),Mage :: getModel('customer/session')等.当我想使用会话作为存储时,我应该选择哪个会话类?又为什么呢?我只是感到困惑.

I observed that there are more than one session class in Magento, for example, Mage::getModel('core/session'), Mage::getModel('customer/session') and so on. When I want to use session as a storage, which session class should I choose? And Why? I'm just confused.

推荐答案

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);

这篇关于如何在Magento中使用Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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