How to create custom helper in Magento 2

First of all, you need to create new custom module with following required files before you add custom helper class.

  1. etc/module.xml
  2. registration.php
  3. composer.json (optional)

Lets start to create module for helper class with required files.

create the app/code/Mageclues/Custom/etc/module.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="Mageclues_Custom" setup_version="1.0.0">
    </module>
</config>

Then, create the app/code/Mageclues/Custom/registration.php file.

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

Then, create the app/code/Mageclues/Custom/composer.json file.

{
   "name": "mageclues/module-custom",
   "description": "",
   "require": {
       "php": "~5.5.0|~5.6.0|~7.0.0",
       "magento/framework": "100.0.*",
       "magento/module-ui": "100.0.*",
       "magento/module-config": "100.0.*",
       "magento/module-directory": "100.0.*"
   },
   "type": "magento2-module",
   "version": "100.0.0",
   "license": [
       "OSL-3.0",
       "AFL-3.0"
   ],
   "autoload": {
       "files": [ "registration.php" ],
       "psr-4": {
           "Mageclues\\Custom\\": ""
       }
   }
}

Finally, we create custom helper class in Magento2. Please create app/code/Mageclues/Custom/Helper/Data.php file.

namespace Mageclues\Custom\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{

public function __construct(
        Context $context
    ) {
        parent::__construct($context);
    }
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *