Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

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:

MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\etc

MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\Helper

MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\composer.json

MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\registration.php

MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\etc\module.xml

MAGENTO_ROOT\app\code\[Module_Namespace]\[Module_Name]\Helper\Data.php

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"],
    "autoload":
    {
        "files": ["registration.php"],
        "psr-4": {"Cloudways\\Customhelper\\": ""}
    }
}


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

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Cloudways_Customhelper',
    __DIR__
);


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>
</config>


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()
    {
        $this->helper->HelperFunc();
    }
}


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.

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:
helpers ,magento 2

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}