Question

I've made a new module called test_helper and I want to create a helper class in this module so that I can add custom functions to be called all over my site.

I can't however seem to find any examples of just how to go about doing this. I assume I have to create some extra xml in config and ad another file somewhere that extends a base helper but I've not had any luck finding an example to build upon.

Was it helpful?

Solution

Your module naming convention is quite confusing - you're calling the module itself helper? For the purpose of explaining, I'm choosing to call your module myname_mymodule

In your module ./app/code/community/MyName/MyModule/etc/config.xml, within the <global> tags

<helpers>
  <mymodule>
      <class>MyName_MyModule_Helper</class>
  </mymodule>
</helpers>

Then create the file ./app/code/community/MyName/MyModule/Helper/Data.php

<?php

class MyName_MyModule_Helper_Data extends Mage_Core_Helper_Abstract{

}

Then to call that module, you would use

$helper = Mage::helper('mymodule');

OTHER TIPS

Add a directory Helper in the extension directory and in there a file Data.php

class Test_Helper_Helper_Data extends Mage_Core_Helper_Abstract {

  public function yourFunction() {
     ...
     your code here
     ...
  } 

}

Now you can call it via

Mage::helper('test/helper')->yourFunction();

Also add the following in your config.xml

...
<global>
    ...
    <helpers>
        <[extension name]>
            <class>[Namespace]_[extension name]_Helper</class>
        </[extension name]>
    </helpers>
    ...
</global>
...
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top