Published On: June 30th, 20184.2 min read

Magento 2 is a popular eCommerce platform used for online stores. Today we are going to see how to build a simple “Hello World” module in Magento 2.

Prerequisite of this tutorial:

Magento 2.1.x installed along with Apache2, correct Php and MySql version supported by Magento 2.1.x.

Follow this link for setup and installation guide:

  1. After Installation, disable all Magento 2 cache: Disabling Magento 2 cache during development will save you some time because you won’t need to manually flush the cache every time you make changes to your code. The easiest way to disable cache is to go to Admin → System → Cache Management → select all cache types and disable them.
  2. Turn on Developer mode: This will allow you to see all the errors Magento 2 will throw during application development. Open the terminal and go to your Magento 2 project directory. Type the following command and press enter:
    php bin/magento deploy:mode:set developer

Creating the “Demo_HelloWorld” Module:

The module name is defined as “VendorName_ModuleName”. First part is the name of the vendor and last part is the name of the module. In our case, the module name is Demo_HelloWorld. In order to create the module go to your app directory. Inside app->code create folders according to the following structure.

Demo->HelloWorld. Your final directory should look like this app/code/Demo/HelloWorld.

Inside the Demo/HelloWorld folder that you have just created, make a folder name etc. Inside etc add a file module.xml. The final structure is app/code/Demo/HelloWorld/etc/module.xml

Edit module.xml with the following code:

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">



After this, we need to register our module to use it so we will have to create a registration.php file.

In the HelloWorld folder in app/code/Demo/HelloWorld create a filename registration.php

Edit the registration.php using the following code







Now we have to enable the module. Before that let's check the status of the module.

Inside the project directory opens the terminal and type:

php bin/magento module:status

You will see in the output the module is disabled now:

List of disabled modules: Demo_HelloWorld

Type in the terminal to enable it:

php bin/magento module:enable Demo_HelloWorld

It’s time to create the controller now. However, before this, we need to define the route.

In Magento, routes are divided into 3 parts: Route front name, controller, and action. For example:

To add a route, it is necessary to create routes.xml file.

Create the following routes.xml file in the specified directory app/code/Demo/HelloWorld/etc/frontend/routes.xml

Magento has an admin panel too which also has its individual routes. To separate the frontend routes the routes.xml file is created inside the frontend folder. But for the backend routes which we do not need right now, it is created inside app/code/Demo/HelloWorld/etc/adminhtml/routes.xml.

Open the file app/code/Demo/HelloWorld/etc/frontend/routes.xml and paste the below code:

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">






After this, we will create the controller and action

The folder and file you need to create is:


Here, the controller is “index” and action is “say hello”.

So as mentioned earlier our overall URL according to this format becomes http:///helloworld/index/sayhello.

Open the SayHello.php and edit with the following code.


namespace DemoHelloWorldControllerIndex;

class SayHello extends MagentoFrameworkAppActionAction


protected $_pageFactory;

public function __construct(

MagentoFrameworkAppActionContext $context,

MagentoFrameworkViewResultPageFactory $pageFactory)


$this->_pageFactory = $pageFactory;

return parent::__construct($context);


public function execute()


echo "Hello World";




Now inside your project directory run the following command:

sudo php bin/magento setup:upgrade

sudo php bin/magento setup:di:compile

sudo php bin/magento setup:static-content:deploy

sudo php bin/magento cache:clean

sudo chmod -R 777 .

*For development mode, I like to give all 777 permission which is not permissible in production

For development mode, I like to give all files and folder 777 permission to avoid constant write permission error. However, for production following files and folder permission should be used.


find . -type f -exec chmod 644 {} ;                       

// 644 permission for files

find . -type d -exec chmod 755 {} ;                       

// 755 permission for directory

find ./var -type d -exec chmod 777 {} ;               

// 777 permission for var folder   

find ./pub/media -type d -exec chmod 777 {} ;

find ./pub/static -type d -exec chmod 777 {} ;

chmod 777 ./app/etc

chmod 644 ./app/etc/*.xml

chown -R : .

chmod u+x bin/magento

We are done creating our Demo_HelloWorld module. Open your browser and visit the following URL: http:///helloworld/index/sayhello

You will see Hello World as output.

Source link:

Contributor: Syed Ishtiaque AhmadNascenia

Share it, Choose Your Platform!

More to Explore

The Quest for Knowledge Continues. Fuel Your Curiosity.