Tariq Aziz
Tariq Aziz

Reputation: 798

Call a custom module helper from template file of another module

I have created an new module. The module is connected with another database. Now I want to call the helper class from another module template file let say "description.pthml". I used the following code.

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

But I am getting error "Fatal error: Class 'Mage_ForumProdPosts_Helper_Output' not found in /home/black/public_html/app/Mage.php on line 546".

The helper class is located in local/MyWebsite/ForumProdPosts/Helper/Output.php having the following code.

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;
    }
}

Also the config.xml of my module is

<?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>

Seems to me the magento not recognizing my module. Help me what I am doing wrong.

I have just realized that my module is not appearing in the configuration/advanced section of admin panel. I have done reindexing and clear cashe also the MyWebsite_ForumProdPosts.xml in etc/modules have the follwing code.

<?xml version="1.0"?>

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

Upvotes: 0

Views: 1201

Answers (2)

Simon LX
Simon LX

Reputation: 31

Mage::helper() method works with the xml nodes in the config.xml, not with the name of your module.

When Magento starts, all the config.xml are concatenated in one big xml file. In this file, the node global/helpers includes all the helpers defined in all your modules. The helper method use those nodes to load the right class :

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

So here, to access to MyWebsite_ForumProdPosts_Helper_Output, you've to write :

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

Upvotes: 1

Anant
Anant

Reputation: 3077

I think the naming convention must match the name defined in the config.xml. So try the code given below

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

Upvotes: 1

Related Questions