从另一个模块的模板文件中调用自定义模块助手

时间:2013-02-15 13:46:48

标签: php magento module

我创建了一个新模块。该模块与另一个数据库连接。现在我想从另一个模块模板文件调用helper类,让我们说“description.pthml”。我使用了以下代码。

$_helper = $this->helper('ForumProdPosts/output');

但是我收到错误“致命错误:在第546行的/home/black/public_html/app/Mage.php中找不到类'Mage_ForumProdPosts_Helper_Output'。”

帮助程序类位于local / MyWebsite / ForumProdPosts / Helper / Output.php中,具有以下代码。

class MyWebsite_ForumProdPosts_Helper_Output extends Mage_Core_Helper_Abstract
{
    /**
     * Constructor
     */
    public function __construct()
    {
        Mage::dispatchEvent('forumprodposts_helper_output_construct', array('helper'=>$this));
    }

    public function getForumPosts(){
        echo "I m here";
            exit;
    }
}

我模块的config.xml也是

<?xml version="1.0"?>
<config>
    <modules>
        <MyWebsite_ForumProdPosts>
            <version>0.1.0</version>
        </MyWebsite_ForumProdPosts>
    </modules>
    <frontend>
        <routers>
            <forumprodposts>
                <use>standard</use>
                <args>
                    <module>MyWebsite_ForumProdPosts</module>
                    <frontName>forumprodposts</frontName>
                </args>
            </forumprodposts>
        </routers>
        <layout>
            <updates>
                <forumprodposts>
                    <file>forumprodposts.xml</file>
                </forumprodposts>
            </updates>
        </layout>
    </frontend>
    <global>
        <helpers>
            <forumprodposts>
                <class>MyWebsite_ForumProdPosts_Helper</class>
            </forumprodposts>
        </helpers>
        <resources>
            <forumprodposts_write>
                <connection>
                    <use>phpbb_database</use>
                </connection>
            </forumprodposts_write>
            <forumprodposts_read>
                <connection>
                    <use>phpbb_database</use>
                </connection>
            </forumprodposts_read>
            <forumprodposts_setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </forumprodposts_setup>
            <forumprodposts_database>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[user]]></username>
                    <password><![CDATA[password]]></password>
                    <dbname><![CDATA[forumdb]]></dbname>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </forumprodposts_database>
        </resources>
    </global>
</config>

对我来说,magento没有认出我的模块。帮助我做错了什么。

我刚刚意识到我的模块没有出现在管理面板的配置/高级部分中。我已经完成了重建索引并清除了cashe,而etc / modules中的MyWebsite_ForumProdPosts.xml也有以下代码。

<?xml version="1.0"?>

<config>
    <modules>
        <MyWebsite_ForumProdPosts>
            <active>true</active>
            <codePool>local</codePool>
        </MyWebsite_ForumProdPosts>
    </modules>
</config>

2 个答案:

答案 0 :(得分:1)

我认为命名约定必须与config.xml中定义的名称匹配。 所以尝试下面给出的代码

$_helper = $this->helper('forumprodposts/output');
// all in small letter as defined in your xml file

答案 1 :(得分:1)

Mage::helper()方法适用于config.xml中的xml节点,而不适用于模块的名称。

当Magento启动时,所有config.xml都连接在一个大的xml文件中。在此文件中,节点global / helpers包含所有模块中定义的所有帮助程序。辅助方法使用这些节点加载正确的类:

public static function helper($name)
{
    $registryKey = '_helper/' . $name;
    if (!self::registry($registryKey)) {
        $helperClass = self::getConfig()->getHelperClassName($name);
        self::register($registryKey, new $helperClass);
    }
    return self::registry($registryKey);
}

所以在这里,要访问MyWebsite_ForumProdPosts_Helper_Output,你要写:

$_helper = $this->helper('forumprodposts/output');