The need of helpers

For developing a Magento website, we often need some functions to be called all over the website. For that purpose we create helper class and helper functions inside the helper class.

What are helpers

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. Helpers are classes that provide functionalities for various features throughout the Magento website. We can use them in controllers, views, models and even in other helpers. Helpers are just collections of unrelated methods and are always instantiated as a singletons. Also Magento requires a helper for each module for translation reasons. You can just create a helper called Data.php in each module and leave it empty.

Helpers in Magento 1.x

In Magento 1.x ,we have to specify helper in config.xml of module.

In your module ./app/code/community/Vendor_Name/Module_Name/etc/config.xml, within the tags

Then create the file ./app/code/community/Vendor_Name/Module_Name/Helper/Data.php

In Magento 1.x, if we created a helper class named Data, then we have to call it using statement like:
$helper = Mage::helper(‘modulename’);

Helpers in magento 2.x

The helper creation in magento 2.x is a little bit different from magento 1.x.

Creating Helper

We need to create the following directory structure:

Let us check the example of creating a sample helper function “doSomething()”. In the Data.php add the following code

The helper has been created successfully. This is a tiny class with a single function:doSomething(). In constrast with Magento 1.x, we don’t need to declare our helpers in a config.xml file or something. Magento 2.x can locate it’s helpers on it’s own. In the above Data.php, you can see that we have created a function doSomething() that can be called anywhere in Magento 2.x using Dependency Injection. To use this helper, follow the below code schema:

In the above constructor, Magento will automatically create object of Data class and assign it to reference $helper. This concept is called dependency injection.


Helpers contain functions and methods that are commonly used throughout the Magento application. Methods that are declared as Helpers can be called from any template file, block, model, controller class or even from another Helpers in Magento 2.x.

That’s it! This is a simple way to create a helper function Magento 2.x.