3

I am trying to write a helper class for my module. The helper class will provide two methods with various functionality. Can I put this helper class beside the other helper class in my .../Helper/Data.php file or should I put it somewhere else? Also, how should I reference it from other parts of Magento in order to get access to its methods?

17

You can create as many helpers as you want in your module.
Let's say your module name is Namespace_Module.
The config.xml of your module should contain this inside the <global> tag:

<helpers>
    <module>
       <class>Namespace_Module_Helper</class>
    </module>
</helpers>

Now let's say you want to create a helper called Something.
You should place the code in Namespace/Module/Helper/Something.php:

<?php 
class Namespace_Module_Helper_Something extends Mage_Core_Helper_Abstract { //or any other helper

    public function doSomething(){
        ....
    }
}

You can call your new helper anywhere like this:

Mage::helper('module/something')->doSomething();

Basically Mage::helper('module'); is the same thing as Mage::helper('module/data');

  • If my class is called Slice_CustomCompare_Helper_CookieAccess, should config.xml contain <class>Slice_CustomCompare_Helper</class> or <class>Slice_CustomCompare_Helper_CookieAccess</class>? – fuumind Feb 14 '14 at 15:31
  • 1
    <class>Slice_CustomCompare_Helper</class>. Then call it like this: Mage::helper('customcompare/cookieAccess') – Marius Feb 14 '14 at 15:39
4

You can create a new file with your helper-Class in the /Helper folder of your module. You can load your own helper in the same way as you load the Data-Helper.

If you configured your helpers in etc/config.xml like the following:

<helpers>
   <mynamespace_mymodule>
      <class>MyNamespace_MyModule_Helper</class>
   </mynamespace_mymodule>
</helpers>

...you can load your custom helper MyNamespace_MyModule_Helper_Custom (located in /app/code/local/MyNamespace/MyModule/Helper/Custom.php) by:

Mage::helper('mynamespace_mymodule/custom')

The required MyNamespace_MyModule_Helper_Data can be loaded by:

Mage::helper('mynamespace_mymodule')

or

Mage::helper('mynamespace_mymodule/data')

Why?

public function getHelperClassName($helperName)
{
    if (strpos($helperName, '/') === false) {
        $helperName .= '/data';
    }
    return $this->getGroupedClassName('helper', $helperName);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.