Over a million developers have joined DZone.

How to Create a Helper in Magento 2

DZone's Guide to

How to Create a Helper in Magento 2

In this blog post, I am going to shed light on the technical steps involved in creating a helper in Magento 2. Read on and see how it's done.

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

The helper class contains the collection of methods which allow us to perform common tasks on objects and variables in Magento. It is always instantiated as the singleton. Helper is related to procedural programming with functions which are grouped under a namespace/class. Helpers can also be found in core Magento. It is easy to put those methods in Helpers which we need to call in a lot of places like controllers, views, models, templates, etc.

In this blog post, I am going to shed light on the technical steps involved in creating a helper in Magento 2. It is necessary for us to know how to create a helper in Magento 2 since it is an important development process for any Magento developer.

We will be creating a new module and with the help of this module, we will call a helper. First of all, create the following files and folders to complete the directory structure of a module:







Note: In this blog post, we will be using "Cloudways" as the module namespace and "Customhelper" as the module name.

To create a simple module in Magento 2, you have to create three important files: composer.json, registration.php, and module.xml. Below is the content of the composer.json file:

    "name": "cloudways/productlabel",
    "description": "Cloudways Custom helper",
    "require": {"php": "~5.5.0|~5.6.0|~7.0.0"},
    "type": "magento2-module",
    "version": "1.0.1",
    "license": ["Commercial"],
        "files": ["registration.php"],
        "psr-4": {"Cloudways\\Customhelper\\": ""}

Below is the content of the module registration.php file:


And finally, the content for the module config file, module.xml located in MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\etc\ is as below:

<?xml version="1.0"?>
  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
  <module name="Cloudways_Customhelper" setup_version="1.0.1"></module>

Now that you have just created the module, it is necessary to create a helper class. The content of the module helper class Data.php found in MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\Helper\ is as below:

namespace Cloudways\Customhelper\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
    public function HelperFunc()
        return 1;

In the code above, you can see that you have created a function named as "HelperFunc()" and this function can be called anywhere in Magento 2 using Dependency Injection (DI):

class ClassName
    public function __construct(\Cloudways\Customhelper\Helper\Data $helper)
        $this->helper = $helper;

    public function func()

That's all. You have successfully created your helper in Magento 2. I hope you have learned something new and exciting. If you have anything to say, feel free to share it in the comments section.

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.

helpers ,magento 2

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}