`
天梯梦
  • 浏览: 13629780 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

Magento Helper简介

 
阅读更多

正如许多其他的PHP MVC系统一样,Magento也有帮助类(Helper Classes)。这些类用来提供一些不适合放在模型,视图或者控制器中的功能。Magento的帮助类也是采用分组类名的机制。也就是说我们可以覆盖默认的帮助类,同时我们需要在config.xml中指定帮助类的基类名。

 

Magento系统默认模块有一个默认的帮助类。正如我们上面的异常显示,我们的Helloworld模块并没有指定一个默认的帮助类。下面让我们来添加一个。修改config.xml

File: app/code/local/Zhlmmc/Helloworld/etc/config.xml

<!– … –>
<global>
    <!– … –>
    <helpers>
        <helloworld>
            <class>Zhlmmc_Helloworld_Helper</class>
        </helloworld>
    </helpers>
    <!– … –>
</global>
<!– … –>
 

你现在应该对这类配置相当熟悉了。"<helloworld>"节点就是模块的名字,"<class>"就是帮助类的基类名,命名方式如下

Packagename_Modulename_Helper

 

帮助类是通过全局对象Mage的静态方法“helper”来装载的。

 

Mage::helper("helloworld/foo")

根据我们的配置,上面这行代码将会装载以下类

app/code/local/Zhlmmc/Helper/Foo.php

class Zhlmmc_Helloworld_Helper_Foo

 

我们上面说过Magento默认每个模块有一个帮助类“data”

Mage::helper('helloworld');
Mage::helper('helloworld/data');

面这两行代码是等价的,都会装载以下类

app/code/local/Zhlmmc/Helper/Data.php

class Zhlmmc_Helloworld_Helper_Data

 

下面我们来创建我们的帮助类

File: app/code/local/Zhlmmc/Helper/Data.php

class Zhlmmc_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract
{
} 
 

清空Magento缓存,重新装载页面,你会发现错误不见了,但是我们的标签页还是没有出来。如果你好奇帮助类究竟能干什么,建议你去看看“Mage_Core_Helper_Abstract”类。

 

来源:http://www.magentobbs.com/?q=content/magento-helper%E7%AE%80%E4%BB%8B

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics