在magento中调用我的模块中另一个模块的助手

发布于 2024-12-25 05:34:03 字数 258 浏览 1 评论 0原文

如何在我的模块中调用另一个模块的助手?

当我尝试

Mage::helper('helperclass')->getValueClass('',$id)

时,它给我错误:

致命错误:在 C:\wamp\www\example\app\Mage 中找不到类“Mage_Helperclass_Helper_Data” .php 第 516 行

帮助程序类名称为 Test_Helperclass_Helper_Data。

How can I call helper of another module in my module?

When I try

Mage::helper('helperclass')->getValueClass('',$id)

It gives me error:

Fatal error: Class 'Mage_Helperclass_Helper_Data' not found in C:\wamp\www\example\app\Mage.php on line 516

The helper class name is Test_Helperclass_Helper_Data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2025-01-01 05:34:03

这是调用助手的正确方法,但您的错误表明您没有正确设置模块。我假设您的模块存在于 app/code/local/Test/Helperclass 中,并且您已使用 app/etc/modules/Test_Helperclass.xml 文件启用了您的模块。

当您调用 Magento 的工厂时,例如 Mage::getModel()Mage::getSingleton()Mage::helper() ,您不提供完整的类名,而是提供对要实例化的类的引用。

其格式为模块名/类名

在我们的例子中,模块名称是 helperclass (不必与模块的实际名称有任何关系,它也可以是 foobar),我们的类名称是data。因此,我们本质上是在调用 Mage::helper('helperclass/data'),但 Magento 让我们将其缩短为 Mage::helper('helperclass')

我们需要告诉 Magento 扩展 helperclass/data -> 背后的规则Test_Helperclass_Helper_Data。我们在 app/code/local/Test/Helperclass/etc/config.xml 的模块配置文件中执行此操作:

<?xml version="1.0"?>
<config>
    ...
        <global>
            <helpers>
                <!-- Here is where we define the mapping rule -->
                <helperclass>
                    <class>Test_Helperclass_Helper</class>
                </helperclass>
            </helpers>
        </global>
    ...
</config>

This is the correct way to call a helper, but your error suggests that you've not set up your module properly. I presume your module exists in app/code/local/Test/Helperclass, and that you have enabled your module with a app/etc/modules/Test_Helperclass.xml file.

When you call Magento's factories, such as Mage::getModel(), Mage::getSingleton(), or Mage::helper(), you don't provide the full class name, but a reference to the class you'd like to instantiate.

This has the format modulename/classname.

In our case, the module name is helperclass (doesn't have to bare any relationship to the actual name of the module, it could just as well be foobar), and our class name is data. So we're essentially calling Mage::helper('helperclass/data'), but Magento lets us shorten that down to Mage::helper('helperclass').

We need to tell Magento the rule behind expanding helperclass/data -> Test_Helperclass_Helper_Data. We do this in the module configuration file at app/code/local/Test/Helperclass/etc/config.xml:

<?xml version="1.0"?>
<config>
    ...
        <global>
            <helpers>
                <!-- Here is where we define the mapping rule -->
                <helperclass>
                    <class>Test_Helperclass_Helper</class>
                </helperclass>
            </helpers>
        </global>
    ...
</config>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文