How to build hello world Magento 2 module from scratch


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,  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

hello world magento 2 module

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


Use the following code so that Magento 2 will understand module name is Helloworld and its version is 1.0.0

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

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

To view installed but disabled modules use the command:

To enable module, run this command in your terminal:

To Disable module, run this command in your terminal:

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:

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:

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:

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

Go to browser, enter this url: and see the result

If you have done making change but nothing happen, try to clear Magento cache.

Now practice yourself!

How to build hello world Magento 2 module from scratch
4.83 (96.67%) 6 votes


About Author

Daniel is a Magento developer with 5 years of experience with customizing Magento extensions, Magento templates. He's main author at Magento explorer

1 Comment

Leave A Reply