How to create custom helper in Magento 2

- Fire development, Magento 2

Magento 2 Development

First of all, you should create the app/code/Company1/Module1/composer.json file in order to create custom helper in Magento 2:

Then, create the app/code/Company1/Module1/registration.php file

And make app/code/Company1/Module1/etc/module.xml

You’ve just created a module, and finally it is necessary to create a helper class inside the Helper folder. Create app/code/Company1/Module1/Helper/Data.php

Use it inside controller as follows:

Inside the block:

As you can see, $this->helper is an instance of Data. To overwrite classes, use preference.


Alternatively, you can also use plugin, since it is the best way to avoid rewrite conflict. For further information, examine this link.

Source (Stackexchange Question)(Sohel Rana)

More tips from the Cookbook