Helpers are classes that can be used anywhere we want. We can use them in controllers, views, models and even in other helpers.
Helpers are usually created to provide methods for a common functionality. For example, we can create a helper that is used to create logs in our application.
In Magento 1.x
In Magento 1.x, if we created a helper class named Data, then we have to call it using statement like:
$helper = Mage::helper("core/data");
In Magento 2.x
Magento 2 introduced the concept of dependency injection (DI). DI states that instead of creating an object yourself, the environment will create and provide you the object.
For example, if we write a controller class like this:
use \Magento\Framework\App\Action\Action; useĀ \Magento\Framework\App\Action\Context; class Help extends Action { public function __contruct(Context $context) { } }
In the above constructor, Magento will automatically create object of Context class and assign it to reference $context. This concept is called dependency injection.
Let us now create a helper in Magento 2.
Creating Helper
- I assume you already have a module, it not, click here to create one.
- We need to create the following directory structure:
<Mymodule> <Test> <Controller> <Basic> Help.php <etc> <frontend> routes.xml <Helper> Data.php module.xml registration.php
- Create a new folder named Helper under the Test folder.
- Create a new file named Data.php and create a class named Data (or any name, Data is common between developers). Add following code to this file:
<?php namespace Mymodule\Test\Helper; use \Magento\Framework\App\Helper\AbstractHelper; class Data extends AbstractHelper{ public function isNumber($x){ return is_numeric($x) ? "yes" : "no"; } }
- Next, to use this helper, we will create a controller class. You can check this link to know how to create controllers.
Create a folder named Controller under Test. Then create a new folder named Basic under Controller. Now, create a new file Help.php which will have our controller class. Add following code to this file:<?php namespace Mymodule\Test\Controller\Basic; use \Magento\Framework\App\Action\Action; use \Magento\Framework\App\Action\Context; use \Mymodule\Test\Helper\Data; class Help extends Action { var $helper; public function __construct( Context $context, Data $helper ) { $this->helper = $helper; parent::__construct($context); } public function execute() { $first = "a12"; $second = "123"; echo "Is $first a number? " . $this->helper->isNumber($first) . "<br/>"; echo "Is $second a number? " . $this->helper->isNumber($second) . "<br/>"; } } ?>
- Next, we will create (if not exist) the file routes.xml under the etc\frontend folder. Add following code to it:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> <router id="standard"> <route id="hayo" frontName="hayo"> <module name="Mymodule_Test" /> </route> </router> </config>
- Now, we are ready to check. Clear your cache (remove cache, generation folder under var) and visit the URL:
http://mag2.com.local/hayo/basic/help
That’s it!
Hello sir, creating helper in magento was quite easy as i learnt it from this tutorial http://magenticians.com/create-helper-magento and your article has made creating helper in magento 2 look more easier. Thanks for the post.
Thanks a lot for this, very helpful! had most annoying problems with getting my controller to inject/find the helper class, your example solved the problem for me.