magento session 和注册变量的方法

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

magento 设置Session

/*
*www.weicot.com
*1050653098@qq.com
*兔子
*/
//输入你要记录Session名称的值
//例 Session的名称是TestString,值为
Mage::getSingleton(‘core/session’)->set TestString(‘weicot’);

取得Session值

//使用get取得已设置Session的值
//例 取得名称为 TestString 的 Session
Mage::getSingleton(‘core/session’)->getTestString();

清除Session值

//使用unset清除已设置Session的值
//例 清除名称为 TestString 的 Session
Mage::getSingleton(‘core/session’)->unsTestString();

参考资料:Magento 的session 用法 和 注册变量的用法

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

Magento’s Global Variable Design Patterns

转载请注明:(●–●) Hello.My Weicot » magento session 和注册变量的方法

文章来源于互联网:magento session 和注册变量的方法

如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。