Magento 2: Guide to creating a Helper Class
Learn how to create a Helper Class in Magento 2 in this tutorial. Helper Classes, a common feature in Magento 1, offer functionalities for various elements throughout a Magento website. Magento 2 allows for the use of Helper Classes in controllers, models, views, and other helpers. This tutorial will provide an introduction to Magento 2 Helper Classes and guide you through the process of creating and implementing them.
Why need to Create a Helper
Helpers serve as globally accessible elements and can be implemented as single instances. They can be called from any location once they are injected into a class. Helpers are typically created to provide methods for common functionality. For example, using helpers to create logs in a Magento application.
What is Helper
In the earlier versions of Magento 2, a Helper Factory was available, which enabled developers to instantiate helper methods. Additionally, the ObjectManager can be used to instantiate the Helper Factory using the code provided.
$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');
However, this code has some limitations. Fortunately, Magento 2 has introduced a more efficient concept called Dependency Injection.
With Dependency Injection, the system creates and supplies objects instead of instantiating them. For instance, if a class is written as class Helper as shown below
class Helper { public function __contruct(Helper $xyz) { $this->xyz= $xyz; } }
In the Helper class constructor, an object of the Helper class is automatically created and assigned to the $xyz reference. This is an example of Dependency Injection.
This concept allows for loose coupling of high-value modules in Magento 2. To include it in a specific class, add the object to its constructor. However, it’s important to note that a dependency can not be injected twice.
How to Create a Helper Class
If you are unsure of how to create a module, please refer to the guide on “How to create a module in Magento 2” first.
Once the module is created, a Helper folder should be added to its directory. The directory structure should resemble the following
app/code/Vendor_Name/Module_Name/Helper/Data.php
The code of the Data.php file:
<?php namespace Vendor_Name\Module_Name\Helper; use \Magento\Framework\App\Helper\AbstractHelper; class Data extends AbstractHelper { public function getStoreConfig() { return true; } }
Once the above steps have been completed, the helper will have been created successfully. The Data.php file will include a function named getStoreConfig() that can be utilized throughout Magento 2 by utilizing Dependency Injection.
In addition, to utilize the helper that has been created, please use the following code format:
<?php use \Vendor_Name\Module_Name\Helper\Data; class Data { public function __construct(Data $helper) { $this->helper = $helper; } public function newFunction() { $this->helper->getStoreConfig(); } }
Conclusion
In summary, the Helper Class contains multiple functions and methods that are commonly utilized across the application. These methods, which have been designated as Helpers, can be accessed from any location in Magento 2, including files, models, blocks, controller classes, or other helpers. I hope this article has given you a thorough understanding of the Helper Class.