欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

magento 设立全局变量或函数 (session, registry 和 function)

更新时间: 2014-01-05 02:58:48 责任编辑: Author_N1

 

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

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

更多参考: 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 version="1.0"?>
<config>
    <modules>
		<Mycode_Function>
			<active>true</active>
			<codePool>local</codePool>
		</Mycode_Function>
    </modules>
</config>
 

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

<?xml version="1.0"?>
<config>
 
    <modules>
        <Mycode_Function>
            <version>1.0.0</version>
        </Mycode_Function>
    </modules>
 
    <global>
 
        <helpers>
            <function>
                <class>Mycode_Function_Helper</class>
            </function>
        </helpers> 
 
    </global>
 
</config>
 

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

<?php
 
class Mycode_Function_Helper_Data extends Mage_Core_Helper_Abstract
{
 
	public function test(){
 
	return 'works';
 
	}
 
}
 

You can now call this function like so

<?php 
 
	 echo Mage::helper('function')->test();
 
 ?>
 

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

 

 

本站相关:

 

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

2. magento 模块重写

 

 

 

 

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:58:48
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/16917/
本文WWW.DOC100.NET DOC100.NET版权所有。