您的位置:首页 > 其它

magento 设置全局变量 (Session 和 Registry)

2016-03-09 14:31 281 查看
1. Magento: Get and set variables in session

To set a Magento session variable:
$myValue = 'Hello World';
Mage::getSingleton('core/session')->setMyValue($myValue);


To Retrieve:
$myValue = '';
$myValue=Mage::getSingleton('core/session')->getMyValue();


To Unset:
Mage::getSingleton('core/session')->unsMyValue();


或者
/* Core Session */
Mage::getSingleton('core/session')->setYourVariable('data');
$Data = Mage::getSingleton('core/session')->getYourVariable();

/* Customer Session */
Mage::getSingleton('customer/session')->setYourVariable('data');
$Data = Mage::getSingleton('customer/session')->getYourVariable();

/* Admin Session */
Mage::getSingleton('admin/session')->setYourVariable('data');
$Data = Mage::getSingleton('admin/session')->getYourVariable();


2. Magento’s Registry Pattern

The three registry methods are
Mage::register
Mage::unregister
Mage::registry


The
register
method is how you set a global-like variable.
Mage::register('some_name', $var);


Then, later in the request execution, (from any method), you can fetch your variable back out
$my_var = Mage::registry('some_name');


Finally, if you want to make you variable unavailable, you can use the
unregister
method to remove
it from the registry.
Mage::unregister('some_name');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: