Magento Create Helper: First Question that comes into our mind is – What is helper in magento and then what are it’s uses? where and how to use them? Here in this tutorial you will get to know everything step by step. Here in this tutorial we are going to explain how you can create helpers and use them.
Magento Create Helper : Custom Helper Example
Helper is basically an object which contains functions. You can call it’s function anywhere such as controller, model or view by just loading helper.
For example –
Magento load and call helper Example :
// load helper $helper = Mage::helper('testhelper'); $helper->myFunction(); |
Now let us understand how to create helper in magento step by step –
Magento : Create Helper Steps
Step 1 : Add Configuration in Config.xml
Go to config.xml and the following in global tag as –
Magento Helper Configuration :
<global> //... //... //...other config goes here <helpers> <testhelper> <class>MyCompany_Test_Helper</class> </testhelper> </helpers> </global> |
Step 2 : Create Helper Class
Let us create helper class. Go to your module folder and create folder named as “helper” and create new file as data.php ie. app/code/local/MyCompany/Test/Helper/data.php. Now let us create helper class as below –
Magento Create Helper Class Example :
<?php class MyCompany_Test_Helper_Data extends Mage_Core_Helper_Abstract{ public function myFunction(){ echo "Helper Function Called."; } } ??> |
Make sure class name MyCompany_Test_Helper_Data is correct.
Step 3 : Load Helper & Call Function –
Now first load the helper and then you can call your helper function as below-
Magento load and call helper Example :
// load helper $helper = Mage::helper('testhelper'); $helper->myFunction(); |
testhelper is tag name defined in config.xml which is used to load helper in magento.
More About
Let’s have look over more about magento helper here.
Magento helper class not found
Sometimes we get the error helper class not found in magento. This error comes when you have defined the helper in config.xml or the class name defined is not correct. So make sure you have defined both correctly. I am sure it will work if both are correct.