Magento Helper

A Magento Helper is a PHP class in Magento which can be used to call reusable functions all over the site. Anyone (block, controller, model, template) or anywhere you can call a helper to use its functions.

To call a Helper, Syntax is

$helper = Mage::helper(‘helper_key’)->myFunction();

Helper key is defined in its module directory etc/config.xml of your module.

You can call a helper like this also,

$helper = Mage::helper(‘helper_key/data’)->myFunction(); //equivalent to the above syntax

Please find the sample code below to create custom Magento helper of your custom module:

1) First, make an entry in config.xml like below to register Magento helper in your custom module. The entry must be within <global> tag to make helper as globally accessible and after </blocks> tag if you are using blocks.
Location: app/code/local/Mynamespace/Mymodule/etc/config.xml
<mytest> ==my custom helper key==

2) Next, create the file with the following directory:

Location: app/code/local/Mynamespace/Mymodule/Helper/Data.php

class Mynamespace_Mymodule_Helper_Data extends Mage_Core_Helper_Abstract
public function myFunction()
return “test helper called”;

3) Now call your custom Magento helper from anywhere with the following syntax. Mage::helper(‘mytest’)->myFunction(); // will output text as “test helper called”

That’s it! Your Custom Magento helper is working fine.

Leave a Reply