We previously created a module, we will now learn how to use a helper in it !
What is a Helper ?
As the name suggests, a "helper" is something that is done to help! It is an object that contains functions that are going to be practical and you can call from anywhere.
For example :
$helper = Mage::helper('pfay_films');
Note that this call is actually equivalent to:
$helper = Mage::helper('pfay_films/data');
By default the helper "data" is called.
Create our Helper
As usual it starts by declaring it in the config.xml of your module in <global>
<helpers>
<pfay_films>
<class>Pfay_Films_Helper</class>
</pfay_films>
</helpers>
Then creates the folder and the file app/code/local/Pfay/Films/Helper/Data.php which takes as argument a number and returns this number doubled. A helper is an object that extends
Mage_Core_Helper_Abstract.
class Pfay_Films_Helper_Data extends Mage_Core_Helper_Abstract
{
public function foisdeux($nbr)
{
return $nbr*2;
}
}
Now in our Block /app/code/local/Pfay/Films/Block/Monblock.php let's call the methodblock function by :
class Pfay_Films_Block_Monblock extends Mage_Core_Block_Template
{
public function methodblock()
{
$helper = Mage::helper('pfay_films');
return '2*2 = '.$helper->foisdeux(2);
}
}
You can now test it ! When you go on http://votresite.com/index.php/films/index/, it works !
That you understand how to create a helper on magento, I invite you to practice, it is the only way to be better;) If you have questions, feel free to leave a comment.
If that does not work for you, you can download my code and compare it with your own to find the difference.