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!
- Why Create Magento Helpers?
- Magento 2 Helpers
- Magento 1 Helpers
- Create and Use Magento 2 Helper
- 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 🙂
Abdul Rehman
Abdul is a tech-savvy, coffee-fueled, and creatively driven marketer who loves keeping up with the latest software updates and tech gadgets. He's also a skilled technical writer who can explain complex concepts simply for a broad audience. Abdul enjoys sharing his knowledge of the Cloud industry through user manuals, documentation, and blog posts.