How to Create a Module in Magento 2

Today’s tutorial are showing how to create a module in Magento 2. When you work in Magento you have to create a custom module at some point. Magento 2 has been around for  2 years now. More people are attracted to Magento’s  powerful features and developing more Magento websites in the eCommerce industry. Magento considered to be one of the best shopping cart solution with all required features for an eCommerce website.

In this tutorial we are showing a  step by step tutorial to create a Magento 2 module. Well you can say it is a HelloWorld module in Magento 2, to understand well the whole process of creating a Magento2 module, We just splits  this  tutorial to different sections

Step #1) Create a module folder
Step #2) Declare the module.xml file
Step #3) Register the module by registratoin.php
Step #4) Enable the module
Step #5) Create route
Step #6) Create Controller and print the hello world text

Step 1) Create a module folder

Those who familiar with Magento1 will take some time to adapt the new folder structure for Magento2. Unlike Magento 1 there is no code pool but all the codes will reside in app/code folder.
Like Magento 1, you will have two nested folders for the module. It means your folder structure would be like this


So we have created two folders

In this example  TutsPlanet is the namespace and HelloWorld is the module name.

Step 2) Declare the module.xml file

Once you have created the module folders you can create the module.xml in etc folder.

Most of the code is part of boiler plate template, you have to write your namespace /module name, version of module properly in <module> tag.
Note: Linux servers are case sensitive so be careful with your folder names.

Step 3) Register the module by registration.php

It is necessary to register your module via ComponentRegistrar in Magento 2. So you have to create registration.php in the


Step 4) Enable the module

Once you have registered your module you can just enable it. Unlike Magento 1 you can do it via command line. Prior to that we can check whether Magento recognized our new module or not.

Run the following command in the command line at your project root. You can see all the enabled and disabled modules there. There you can see your new module under disabled modules.

php bin/magento module:status
List of disabled modules:

Now we can enable the module by running the below command

php bin/magento module:enable TutsPlanet_HelloWorld

But when you are enabling this module first time, you have to run the following command in order to check and upgrade module database.

php bin/magento setup:upgrade

Step 5) Create route

we have successfully created a module, but we don’t have anything to see in the frontend. Now we are going to create something repsonsible for a out put in the browser.

Create your routes.xml in the app/code/TutsPlanet/HelloWorld/etc/frontend/ folder

This code will activate the frontend router for your module. And you can give the name of the router in <route> tag. This name will come after the site url for example.


Step 6) Create Controller and print the hello world text .

Unlike Magento 1, controllers doesn’t have all the actions in it. So you have to create different files for different methods.

So the following code snippet is your content of controller file.

After finished this step go to command line clear the cache.

php bin/magento cache:clean

Now you are ready to see your first module’s output. The URL will be coming as the following pattern.


You can download the sample code from my GitHub repo.




Editorial Staff

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.