6 Simple Steps In Creating Your Own Hello World Module in Magento 2

Posted on: 26 Feb 2020 by Nidheesh KK

To create the Hello World module, you need to perform the simple steps.
Step 1: Create a directory for the module Hello World.
Step 2: Create etc/module.xml file
Step 3: Create registration.php file
Step 4: Create routes.xml file
Step 5: Create controller file and action
Step 6: Enable the module

The name Magento has just started to become familiar for the past 10 years.
Even though in this 10 years of the short period it had tried to bring a massive change in the field of eCommerce.
This success story got onto rail soon the updated version of Magento has come into markets.
Today Magento 2 has become one of the most widely and commonly used eCommerce development platforms around the world.
Well then there are many other eCommerce store builders then what makes Magento more popular?
Simple and easier answer for this is, its an open-source eCommerce Platform with all new updated and advanced features.
Beyond these, there are even more features to explain to find the benefits of Magento 2.
But here we are going to see a very small and easy Magento Module which both the beginner as well as a programmer should know.
Let’s jump into how to create Hello World in Magento 2.

Create Hello World Module in Magento 2

Here our developers will help you in implementing one of the basic Module in Magento.
In this article:
Step 1: Create a directory for the module Hello World.
Step 2: Create etc/module.xml file
Step 3: Create registration.php file
Step 4: Create routes.xml file
Step 5: Create controller file and action
Step 6: Enable the module

Step 1: Create a directory for the module Hello World

Create a folder like below given format:
app/code/Eglobe/HelloWorld
The Eglobe folder is the vender name, and HelloWorld is the module’s name

Step 2: Create etc/module.xml file

Now, create etc folder and add the module.xml file
app/code/Eglobe/HelloWorld/etc/module.xml
Contents would be:

Step 3: Create registration.php file

Create registration.php as follows:
app/code/Eglobe/HelloWorld/registration.php
Code Contents:

After define the route, the URL path to our module will be: http://example.com/helloworld/*

Step 5: Create controller file and action

Create Display.php controller file in the app/code/Eglobe/HelloWorld/Controller/Index folder with the following code:
In this step, we will create controller and action to display Hello World.
Let choose that the url for this action as: http://example.com/helloworld/index/display
So the file we need to create is:
app/code/Eglobe/HelloWorld/Index/Display.php
Content:

<?php
namespace Eglobe\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;
  }
}

Step 6: Enable the module

Open your terminal and go to magento2 root. Run the following command there:
php bin/magento setup:upgrade
sudo chmod -R 777 var pub/static generated
Now open the url http://example.com/helloworld/index/display and you will see Hello World.