Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How To Create And Use Helpers In Magento 2

September 12, 2016

3 Min Read
Reading Time: 3 minutes

We all know Helpers from the days of Magento 1. Helpers are classes that provide functionalities for various features throughout the Magento website. Available for use anywhere on the website, Magento 2 Helper can be called in controllers, views, models and even in other helpers too!

Magento 2 Helper

Why Create Helpers?

Helpers are usually used as elements that are global and always available. They can even be created as a Singleton (single instances of objects). Helpers can be called everywhere and you only need to inject them in the class. Helpers are mainly created to provide methods for the most common functionalities. For example, helpers are used to build logs in the Magento application.

Helpers in Magento 1

In Magento 1, if I want to call a Helper class Data, I use the following statement:

$helper = Mage::helper('core/data');

Given the popularity of the idea, helpers are also available in Magento 2. However, because of the absence of the Mage class, the above statement will produce an error in Magento 2.

Scale Your Magento 2 Store With Ease

One-Click Magento installation with your own managed hosting solution.

Helpers in Magento 2

In the early versions, Magento 2 had a Helper Factory which allowed developers to instantiate helper methods. ObjectManager was required to instantiate the Helper Factory. So, in this case, you could use the following code:

$object_manager = \Magento\Core\Model\ObjectManager::getInstance();
$helper_factory = $object_manager->get('\Magento\Core\Model\Factory\Helper');
$helper = $helper_factory->get('\Magento\Core\Helper\Data');

If you think there is a problem with the code, you are right!. Fortunately, Magento introduced a better concept: Dependency Injection in Magento 2.

The concept of Dependency Injection states that instead of instantiating an object yourself, the environment will create and provide you the object. For example, if I write a class like this:

class ABC
{
   public function __contruct(XYZ $xyz)
   {
       $this->xyz= $xyz;
   }
}

In the constructor of the ABC class, an object of XYZ class is automatically created and assigned the reference, $xyz. This idea is called Dependency Injection.

Through Dependency Injection, Magento 2 provides a high-value concept of loose coupling modules together. When you want to inject into a particular class, you can simply do it by adding an object to its constructor. However, bear in mind that in Magento 2, one dependency is not injected twice.

Create and Use Magento 2 Helper

First, create a simple module in Magento 2. I assume that you already have one.

After creating the module, create a Helper folder in the module directory. Next, create Data.php in the Helper folder. The module’s directory structure will look like:

<Module_Namespace>
|-------- Module_Name/
|---------------- composer.json
|---------------- registration.php
|---------------- etc/
|------------------------ module.xml
|---------------- Helper/
|------------------------ Data.php

Add the following code to the newly created Data.php file:

<?php

namespace Module_Namespace\Module_Name\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
       public function RandomFunc()
       {
               echo "This is Helper in Magento 2";
       }
}

The helper has been created successfully. In the above Data.php, you can see that I have created a function RandomFunc() that can be called anywhere in Magento 2 using Dependency Injection. To use this helper, follow the below code schema:

<?php

use \Module_Namespace\Module_Name\Helper\Data;

class RandomClass
{
       public function __construct(Data $helper)
       {
               $this->helper = $helper;
       }
       public function func()
       {
               $this->helper->RandomFunc();
       }
}

Conclusion

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

Magento Hosting On Managed Cloud Hosting Platform

I hope, the concept of Helper is clear now. Still confused? Drop me a message in the comments below 🙂

Share your opinion in the comment section. COMMENT NOW

Share This Article

Boost Your Magento Store Performance by 5x Times & Maximize Your Sales

Our fastest Magento hosting can help you in growing your business revenue by 500%

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!