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

本文介绍了如何在Magento中使用Session和Registry来设置全局变量。对于Session,通过Mage::getSingleton('core/session')->setVariable()进行设置, Mage::getSingleton('core/session')->getVariable()获取, Mage::getSingleton('core/session')->unsVariable()删除。而Registry模式中,使用Mage::register()设定全局变量,Mage::registry()获取,Mage::unregister()移除。
摘要由CSDN通过智能技术生成

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 theunregister method to remove it from the registry.

Mage::unregister('some_name');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值