How To Create Hello World Module in Magento 2 | Magento Tutorial

How To Create Hello World Module in Magento 2 | Magento Tutorial

The name Magento has just started to become familiar for the past 10 years.

Even though in this 10 years of 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 platform 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.

Lets see how to work with very first programming module to print Hello World in magento 2.


How To Create Hello World Module in Magento 2 | Magento Tutorial

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:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Eglobe_HelloWorld" />
</config>

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:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="display">
            <module name="Eglobe_HelloWorld"/>
        </route>
    </router>
</config>

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.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Youtube

leave a comment