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.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

How To Create And Use Magento 2 Helper

Updated on December 23, 2021

3 Min Read
Magento 2 Helper Banner

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!

  1. Why Create Magento Helpers?
  2. Magento 2 Helpers
  3. Magento 1 Helpers
  4. Create and Use Magento 2 Helper
  5. Conclusion

Why Create Magento Helpers?

Magento 2 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. Magento 2 Helpers are mainly created to provide methods for the most common functionalities. For example, helpers are used to build logs in the Magento application.

Scale Your Magento 2 Store With Ease

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

Magento 2 Helpers

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.

Magento 1 Helpers

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.

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 Magento 2 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();
       }
}

Optimize Magento Speed Like a Pro

Subscribe now and get a free ebook to your inbox.

Thank You

Your Ebook is on it’s Way to Your Inbox.

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

Fayyaz Khattak

Fayyaz, a passionate Motorbike tourist, works as a Team Lead — Magento Community at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. You can contact him at [email protected]

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now