3 Steps to Create Helper Class in Magento 2
In today’s tutorial, I will show you how to create Helper Class in Magento 2.
Helper is probably no stranger to those who used the Magento 1 platform. They are classes which can provide functionalities for numerous features throughout the Magento website. With Magento 2, the Helper can be called in controllers, models, views and even in other helpers. In this post, I will provide you some basic knowledge about Magento 2 Helper Class, how to create and use it.
Table of Content
Why need to Create Helper
Helpers can be considered as global and always available elements. They can even be created as single objects’ instances. Besides, they can be called everywhere once you inject them in the class. Helpers are mainly created to offer methods for the most common functionalities. For instance, you can use helpers to build logs in the application of Magento.
What is Helper
In the early version of Magento 2, a Helper Factory is available, which enables developers to instantiate helper methods. Besides, you can use the below code to use ObjectManager to instantiate the Helper Factory.
$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 still has some problems. Luckily, a better concept has been introduced which is Dependency Injection in Magento 2.
Using this concept, the environment will create and provide you an object instead of instantiating it. For instance, if a class is written like the following:
class Helper
{
public function __contruct(Helper $xyz)
{
$this->xyz= $xyz;
}
}
In the Helper class constructor, an object of Helper class is auto-created and assigned the reference, $xyz. This is Dependency Injection.
Via this concept, high-value loose coupling modules together concept is provided by Magento 2. If you want to inject it into a specific class, just add an object to the constructor of it. However, you need to remember that you cannot inject one dependency twice.
How to Create Helper Class
Step 1: Open your module
The first thing you will have to finish is creating a simple module in Magento 2.
- Step 2: After you have created the module, you will need to create a Helper folder in the directory of that module. Following is how the directory structure looks like:
<Mageplaza>
|-------- HelloWorld/
|---------------- composer.json
|---------------- registration.php
|---------------- etc/
|------------------------ module.xml
|---------------- Helper/
|------------------------ Data.php
- Step 3: In the Helper folder, create Data.php file and add the below code to that file:
<?php
namespace Mageplaza\HelloWorld\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
public function getStoreConfig()
{
return true;
}
}
After finishing the above steps, your helper would be created successfully. You can see in the Data.php, a function called getStoreConfig()
has been created, it can be called anywhere in Magento 2 by using Dependency Injection.
Moreover, in order to use the helper that you have just created, please follow the following code schema:
<?php
use \Mageplaza\HelloWorld\Helper\Data;
class Data
{
public function __construct(Data $helper)
{
$this->helper = $helper;
}
public function newFunction()
{
$this->helper->getStoreConfig();
}
}
Conclusion
In conclusion, Magento 2 Helper Class includes various functions and methods which are used commonly throughout the application. All the methods which have been declared as Helpers can be called anywhere including file, model, block, controller class or from another helper in Magento 2. I hope this post has provided you the all the basic information about Helper Class.
0 comments:
Post a Comment
Thanks