Magento 2 has been released on July 2015 and since then many store owner are changing their platform from Magento 1 to Magento 2, at Magentoexplorer.com, Â we will help both Magento store owners and Magento developers to get familiar with Magento 2 through a series of tutorial while still updating new Magento 1 tutorial as there are many people still using Magento 1 as their platform
Today’s tutorial is how to build hello-world Magento 2 module steps by steps. There are 6 steps to make a complete hello world module for Magento 2
- Step 1: Make module folder:
- Step 2: Add module.xml to declear the module
- Step 3: Create registration.php to register the module
- Step 4: How to Install, Enable or Disable/remove the module
- Step 5: Route of the module.
- Step 6: Controller and action.
As you know, in Magento 1, module folders were located in local/ community/ core/, in Magento 2 the folders can be found in app/code folder
Step 1: Module folder:
Module in Magento 2 include Module Vendor and Module Name. In this tutorial, we will use vendor as Magentoexplorer & module name as Helloworld. So we need create a new folder:  app/code/Magentoexplorer/Helloworld
Step 2: Add module.xml to declear the module
Now we will create a configuration file module.xml in etc folder of the module. From this file, Magento 2 will recognize name and version of module
app/code/Magentoexplorer/Helloworld/etc/module.xml
Use the following code so that Magento 2 will understand module name is Helloworld and its version is 1.0.0
1 2 3 4 | <?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="Magentoexplorer_Helloworld" setup_version="1.0.0" /> </config> |
Step 3: Create registration.php to register the module
We create registration.php in app/code/Magentoexplorer/HelloMagento/registration.php
Put the following code to this file
1 2 3 4 5 6 | <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magentoexplorer_Helloworld', __DIR__ ); |
Step 4:Â Install, Enable or Disable/remove the module
Now we can install the module after creating all the file above. Open your terminal program (like putty, bitvise…) and execute these commands
1 2 | cd [magento_directory] php bin/magento setup:upgrade |
To view installed but disabled modules use the command:
1 | php bin/magento module:status |
To enable module, run this command in your terminal:
1 | PHP bin/magento module:enable Magentoexplorer_Helloworld |
To Disable module, run this command in your terminal:
1 | PHP bin/magento module:disable Magentoexplorer_Helloworld |
Note:  You can also enable module in app/etc/config.php , for example with our Magentoexplorer_helloworld module, put this line to enable the module:
1 2 3 | ... 'Magentoexplorer_helloworld' => 1, ... |
Step 5: Route of the module
To make the module display  “hello world” in frontend, we will add a route (url) for it.
A route (url) of a Magento site comprises the following parts:
1 | http://<magento_site>.com/<router>/<controller>/<action> |
You need to set router name for the module so that you can add more controllers or actions later, now we add route for the module by creating routes.xml file in app/code/Magentoexplorer/Helloworld/etc/frontend/routes.xml . Add the following code to the file:
1 2 3 4 5 6 7 8 | <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="magentoexplorer" frontName="helloworld"> <module name="Magentoexplorer_Helloworld" /> </route> </router> </config> |
Step 6: Controller and action.
Next we will define controller and action for the module. We will now create file index.php in app/code/Magentoexplorer/Helloworld/Index/Index.php and add the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php namespace Magentoexplorer\Helloworld\Controller\Index; class Display extends \Magento\Framework\App\Action\Action { Â public function __construct( \Magento\Framework\App\Action\Context $context) Â { Â Â Â return parent::__construct($context); Â } Â public function execute() Â { Â Â Â echo 'Hello World'; Â Â Â exit; Â } } |
Go to browser, enter this url:Â www.yoursite.com/magentoexplorer/helloworld and see the result
If you have done making change but nothing happen, try to clear Magento cache.
Now practice yourself!
1 Comment
Thank you, it worked like a charm, now I will try to make another Magento module for my checkout page