Monday , August 21 2017
Home >> All tutorial >> Magento Developers’ Guide – Part 12: The Helpers

Magento Developers’ Guide – Part 12: The Helpers

Now, Magento Tutorial will show you the way to create your own helper in Magento.

1. What’s a helper?

“Helper” is an object that will contain practival functions for you and you can call it from anywhere, you just load your helper to use it. For example:

  1. $helper = Mage::helper(‘monhelper’);
Note that it’s equivalent to :
  1. $helper = Mage::helper(‘monhelper/data’);
Indeed, it is the default helper « data » wich is called.
2. Create your own helper
As you are now used to, it starts by declaring it in the config.xml of your plugin, do that in <global> and after </blocks>
  1. <helpers>
  2.    <test>
  3.           <class>Pfay_Test_Helper</class>
  4.     </test>
  5. </helpers>
Then creates the folder and the file app/code/local/Pfay/Test/Helper/data.php function wich will contains the function bytwo($ nbr) with a number as an argument and returns that number multiplied by two. A helper is an object that extends the core class Mage_Core_Helper_Abstract.
  1. <?php
  2. class Pfay_Test_Helper_Data extends Mage_Core_Helper_Abstract{
  3.    public function bytwo($nbr){
  4.         return $nbr*2;
  5.    }
  6. }
Now in/app/code/local/Pfay/Test/Block/monblock.php we change the function by:
  1. <?php
  2. class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
  3. {
  4.       public function methodblock()
  5.       {
  6.             $helper = Mage::helper(‘test’);
  7.             return ‘2*2 = ‘.$helper->bytwo(2);
  8.       }
  9. }
Now when we go on yoursite.com/index.php/test/index/, we see that it works!
Tutorial by: pierrefay.com

 


Ready to foster your knowledge with MAGENTO 2 BASIC GUIDE

The new Order Success combo with Magento 2 Inventory Management, Magento 2 Order Fulfillment, Magento 2 Dropship, Magento 2 Barcode Management and Magento 2 Purchase Management

Is your site still a mess and Magento extensions are not enough to save your site? Save your site with Magento Web Development Services now

magento services


About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *