magento 设置全局变量或函数 (Session, Registry 和 Function)

1. Magento: Get and set variables in session

 

To set a Magento session variable:

Php代码   收藏代码
  1. $myValue = 'Hello World';  
  2. Mage::getSingleton('core/session')->setMyValue($myValue);  
 

To Retrieve:

Php代码   收藏代码
  1. $myValue = '';  
  2. $myValue=Mage::getSingleton('core/session')->getMyValue();  

 

To Unset:

Php代码   收藏代码
  1. Mage::getSingleton('core/session')->unsMyValue();  

 

或者

Php代码   收藏代码
  1. /* Core Session */   
  2. Mage::getSingleton('core/session')->setYourVariable('data');  
  3. $Data = Mage::getSingleton('core/session')->getYourVariable();  
  4.    
  5. /* Customer Session */   
  6. Mage::getSingleton('customer/session')->setYourVariable('data');  
  7. $Data = Mage::getSingleton('customer/session')->getYourVariable();  
  8.    
  9. /* Admin Session */   
  10. Mage::getSingleton('admin/session')->setYourVariable('data');  
  11. $Data = Mage::getSingleton('admin/session')->getYourVariable();  

 

2. Magento’s Registry Pattern

 

The three registry methods are

Php代码   收藏代码
  1. Mage::register  
  2. Mage::unregister     
  3. Mage::registry      
 

The register method is how you set a global-like variable.

Php代码   收藏代码
  1. Mage::register('some_name'$var);  
 

Then, later in the request execution, (from any method), you can fetch your variable back out

Php代码   收藏代码
  1. $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.

Php代码   收藏代码
  1. Mage::unregister('some_name');  
 

更多参考: http://alanstorm.com/magento_registry_singleton_tutorial

 

3.  Create Global Function In Magento

 

This code will allow you to add a function that can be called from anywhere within Magento. It extends the helper class

 

1) Create a file named ‘Mycode.xml’ and copy it to app/etc/modules/ – it should look like this:

Xml代码   收藏代码
  1. <?xml version="1.0"?>  
  2. <config>  
  3.     <modules>  
  4.         <Mycode_Function>  
  5.             <active>true</active>  
  6.             <codePool>local</codePool>  
  7.         </Mycode_Function>  
  8.     </modules>  
  9. </config>  
 

2) Create the directory app/code/local/Mycode/Function/etc and then create a file named ‘config.xml’ In it copy:

Xml代码   收藏代码
  1. <?xml version="1.0"?>  
  2. <config>  
  3.    
  4.     <modules>  
  5.         <Mycode_Function>  
  6.             <version>1.0.0</version>  
  7.         </Mycode_Function>  
  8.     </modules>  
  9.    
  10.     <global>  
  11.    
  12.         <helpers>  
  13.             <function>  
  14.                 <class>Mycode_Function_Helper</class>  
  15.             </function>  
  16.         </helpers>   
  17.    
  18.     </global>  
  19.    
  20. </config>  
 

3) Create the directory app/code/local/Mycode/Function/Helper and then create a file named ‘Data.php’ In it copy:

Php代码   收藏代码
  1. <?php  
  2.    
  3. class Mycode_Function_Helper_Data extends Mage_Core_Helper_Abstract  
  4. {  
  5.    
  6.     public function test(){  
  7.    
  8.     return 'works';  
  9.    
  10.     }  
  11.    
  12. }  
 

You can now call this function like so

Php代码   收藏代码
  1. <?php   
  2.    
  3.      echo Mage::helper('function')->test();  
  4.    
  5.  ?>  
 

来源: http://joe-riggs.com/blog/2011/06/create-global-function-in-magento/

 

 

本站相关:

 

1. 深入理解Magento-第九章-修改、扩展、重写Magento代码

2. magento 模块重写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值