How to Create a Custom Module Helper in Magento 2?

  • author-img Nidhi Arora
  • 6 years

Helpers – as the name suggests it is related to something which is right for you or helps you in certain ways. Helpers are the classes, primarily build to offer different ways for a common functionality in Magento 2. The Magento developers are free to utilize/call these classes from anywhere, such as controllers, views, models and helpers.

As a Magento developer, it is essential for you to know how to create a custom module helper since it is the most crucial development process for you. This blog post is going to throw light on the technical steps on how to create Magento 2 helper functions of custom module:

Module namespace is Eecom & the module name is Customhelper.

And, the directory structure will be like:





Create a  composer.json file with the below content:


"name": "eecom/productlabel",

"description": "Eecom Custom helper",

"require": {

"php": "~5.5.0|~5.6.0|~7.0.0"


"type": "magento2-module",

"version": "1.0.1",

"license": [



"autoload": {

"files": [ "registration.php" ],

"psr-4": {

"Eecom\\Customhelper\\": ""




This is compulsory file for module in Magento 2. It is the same, just change the name of module name with “ Eecom\\Customhelper”.

Create a  registration.php  file  with the below content:







This is a module registration file in Magento 2 and it is also the same, just change the module name  as ‘Eecom_Customhelper’.

Now, create a file named as module.xml  under “Etc” folder having the below content:

<?xml version="1.0"?>

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

<module name="Eecom_Customhelper" setup_version="1.0.1">



This is a module config file in Magento 2.

And, then create a file named as Data.php under Helper folder having the below content:


namespace Eecom\Customhelper\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper


public function CustomHelperFunction($key)


$array = [0 => __('Default'),

1 => __('Magento Certified developers'),

2 => __('Ionic app development')


return $array[$key];



So, as you can see we have created a function in the above file, named as:  CustomHelperFunction ()

Now, we need to call this function in our Magento 2 site like:

echo $this->helper('Eecom\Customhelper\Helper\Data')-> CustomHelperFunction ('1');

And, it will return the value of id that you will pass in the parameter.

That’s it! We hope you’ve enjoyed this coding tour of creating a custom module helper in Magento 2.

Download Blog


Ready to Get started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    1250 Pittsford Victor Road Ste 310
    Pittsford, NY 14534

    Development Center

    Plot no. 10, Rajiv Gandhi
    Chandigarh Technology Park