Magento Helper
Kaleem Ahmad

What is a Magento Helper?

Helper is a Magento class having unrelated methods instantiated as a singleton. This is basically procedure with group of functions under a name space. This name space is called class. We can put our configuration functions in this class. Basically we use helpers for configuration purpose functions which are required in our programming same multiple time we define and use this function according to our needs.

Why need to create Helper?

Helpers can be considered as global and always available it's elements. Helpers can even be created as single object instances in magento. Helper methods can be called everywhere once you inject them in the class. Helpers are mainly created to offer methods for the most common configuration functionalities. For instance, we can use helpers to build logs in the application of Magento.

Helper in Magento2 :

Magento 2 had a Helper Factory which allowed developers to instantiate helper methods and fuctions. ObjectManager was required to instantiate the Helper Factory in magento2. So,in this case, we could use the following code:

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

If you think there is a problem with the code in the page load due to the object manager, you are right! object manager increase page load time. To overcome this problem, Magento introduced a better concept: Dependency Injection in Magento2.

Dependency Injection in magento 2 suggested us that instead of instantiating an object yourself, environment will create and provide object you. For example, if I write a class like this via dependency injection:

<?php
	class Symphisys 
		{ 
			$protected $uvw 
			public function __contruct(UVW $uvw) 
			{ 
				$this->uvw= $uvw; 
		} 
	} 
?>

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

Dependency Injection in Magento 2 provides a high value concept of loosely coupling modules together. When you want to inject into a particular class in any block, model or controller, you can simply do it by adding an object to its constructor of the class. Keep in mind a dependency is not injected twice in any constructor.

How to Create Helper Class in Magento?

To create helper in the magento need to follow some steps.

Step 1:Open Module

To understand a helper in magento creating a simple magento2 module.

The name of the module is defined as VendorName_ModuleName First part is vendor name and last part is module name. For example: Symphisys_CustomerPrice. Here vendor name is Symphisys and module name is HelloWorkd. Please check the following guide to create the folders

Step 2: After module creating, you will need to create a Helper folder in the root directory of that module. Following is how the directory structure looks like in the module:

< Symphisys >
|-------- HelloWorld/
|---------------- composer.json
|---------------- registration.php
|---------------- etc/
|------------------------ module.xml
|---------------- Helper/
|------------------------ Data.php

Step 3: In the Helper folder, create Data class with Data.php file and add the below code to that file:

<?php
	namespace Symphisys\HelloWorld\Helper; 
	use \Magento\Framework\App\Helper\AbstractHelper; 
	class Data extends AbstractHelper 
	{ 
		public function getStoreConfig() 
		{ 
			return true; 
		} 
	}		 
?>

After finishing the above steps,you can see that your helper would be created successfully. You can see in the Data.php file, there is a function called getStoreConfig() has been created function, which can be called anywhere in Magento 2 by using Dependency Injection concept.

Moreover, want to create new fuction in the helper, in order to use the helper that you have just created, please follow the following code schema in helper:

<?php
	use \Symphisys\HelloWorld\Helper\Data; 
	class Data{
        public function __construct(Data $helper) 
		{ 
		$this->helper = $helper; 
		} 	
		public function newFunction() 
		{ 
			$this->helper->getStoreConfig(); 
		} 							
	}		 
?>

Conclusion

Magento 2 Helper Class includes various functions and methods which are used commonly throughout the application in magento. All the Helper methods which have been declared as Helpers can be called anywhere including file, block, model, controller class or from another helper in Magento 2 using dependency injection.

Share
Tags
Categories

Kaleem Ahmad
Sr. Magento Developer
Symphisys
May 08, 2020