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:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
	\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'Eglobe_HelloWorld',
	__DIR__
);

Step 4: Create routes.xml file

create app/code/Eglobe/HelloWorld/etc/frontend/routes.xml with the following code:


    
        
            
        
    

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.