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
<global>
<helpers>
<mytest> ==my custom helper key==
<class>Mynamespace_Mymodule_Helper</class>
</mytest>
</helpers>
</global>

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

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

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s