Blog background
October 12, 2018|Read • 4 Min

How to create a “HELLO WORLD” module in Magento 2?

Written by
Venugopal
Venugopal
Hello-world-module

Listen Full Blog Here

Last Updated: Jul 28, 2026

Key Takeaways

  • »Creating a custom module is the first step toward extending Adobe Commerce functionality.
  • »A Magento module consists of essential files such as registration.php, module.xml, routes, and controllers.
  • »Following Magento coding standards ensures better maintainability, compatibility, and future upgrades.
  • »Proper module structure makes it easier to develop custom features and third party integrations.
  • »Learning module development provides a strong foundation for advanced Adobe Commerce customization.

In this blog post, we have explained how to easily create Hello World module in Magento 2. Take a note that the concept of local/ community/ core/ folders only existed in Magento 1 and we don’t use them in Magento 2.

Follow the below steps to create HELLO WORLD module

All of our custom modules in Magento 2 go under the app/code directory. The first step towards create module magento 2 is to create a folder inside your project directory named after the module. This will be where all the files related to the module will reside. The modules have a 2 part naming structure, i.e, Namespace/Module. This is so that the modules can be organized more efficiently. So, assuming our Namespace is “Codilar” and our first module will be called “HelloWorld”, the directory structure for our module is gonna be app/code/Codilar/HelloWorld/…

So now that we’ve established the directory structure, let’s make a simple module which will show “Hello World” on the content section of the page when we hit the URL “http://mywebsite.com/helloworld/”.

Ok first thing’s first. This is going to be our directory structure for the complete project

So lets start with the registration.php file.
app/code/Codilar/HelloWorld/registration.php

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

Every module must contain this file. This basically tells Magento “Please register my module, it’s name is Codilar_HelloWorld, which is the Namespace and the Module name underscore separated”.

Next we come to the etc/module.xml
app/code/Codilar/HelloWorld/etc/module.xml

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

This file just contains some basic information about the module. Like the module name and module version.

Now the basic files absolutely necessary for any module to work are created. Next we would create a frontend route inside our etc/frontend/routes.xml file which would tell Magento to “Forward the request to our module’s controller whenever the URL is {{base_url}}/helloworld/

app/code/Codilar/HelloWorld/etc/frontend/routes.xml

<?xml version="1.0"?>
<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="helloworld">
<module name="Codilar_HelloWorld" />
</route>
</router>
</config>

Now we make the actual controller where the request would be routed.
app/code/Codilar/HelloWorld/Controller/Index/Index.php

<?php
/**
*
* @package magento2
* @author Codilar Technologies
* @license https://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
* @link https://www.codilar.com/
*/
namespace Codilar\HelloWorld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action {
/**
* @var PageFactory
*/
private $pageFactory;


/**
* Index constructor.
* @param Context $context
* @param PageFactory $pageFactory
*/
public function __construct(
Context $context,
PageFactory $pageFactory
)
{
parent::__construct($context);
$this->pageFactory = $pageFactory;
}


/**
* Execute action based on request and return result
*
* Note: Request will be added as operation argument in future
*
* @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function execute()
{
$page = $this->pageFactory->create();
return $page;
}
}

Now we make our layout file to handle the request
app/code/Codilar/HelloWorld/view/frontend/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Codilar\HelloWorld\Block\Hello" name="helloworld" template="Codilar_HelloWorld::hello.phtml" />
</referenceContainer>
</body>
</page>

The layout file basically “references” the “content” container, and inserts our own block Codilar\HelloWorld\Block\Hellointo it. And remember, blocks and templates always come in pairs. Hence our block also uses the template Codilar_HelloWorld::hello.phtml 

Now to create our block file
app/code/Codilar/HelloWorld/Block/Hello.php

<?php
/**
*
* @package magento2
* @author Codilar Technologies
* @license https://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
* @link https://www.codilar.com/
*/

namespace Codilar\HelloWorld\Block;

use Magento\Framework\View\Element\Template;

class Hello extends Template
{
public function getText() {
return "Hello World";
}
}

Notice that the block has a function getText() . The block’s function within Magento’s MVC architecture is to structure and provide data to the template which the template will then display

And now finally coming to the template file
app/code/Codilar/HelloWorld/view/frontend/templates/hello.phtml

<?php
/* @var \Starlabs\HelloWorld\Block\Hello $block */
?>
<label><?= __("Data retrieved from Block is ") ?></label>
<h1><?= $block->getText() ?></h1>

The template’s job is to fetch data from it’s relevant block (mentioned as an @var comment at the beginning of the template file) and display it accordingly. Ideally, the template should not be responsible for any logical decisions.

Well that’s about it. Now we just run the php bin/magento setup:upgrade command from our terminal to install our new module and then open our favourite browser and type http://mywebsite.com/helloworld/, and we should be able to see our template in action, like this.
Click here to download a zip copy of the above mentioned project

Building Better Adobe Commerce Extensions

Creating a "Hello World" module is more than a beginner exercise. It introduces the core concepts that power every Adobe Commerce customization, from custom payment methods and shipping integrations to advanced business workflows.

As your eCommerce business grows, building scalable, upgrade friendly modules becomes essential for maintaining performance, security, and long term flexibility. Following Adobe Commerce development best practices ensures your customizations remain compatible with future platform updates while reducing technical debt.

Whether you're developing a custom feature or planning a complex Adobe Commerce implementation, partnering with experienced developers can accelerate development and ensure your store is built for long term success. At Codilar, we specialize in custom Adobe Commerce development, integrations, and performance optimization to help businesses deliver exceptional digital commerce experiences.

Liked what you read? Share with your teamShare

FAQs

A Magento 2 module is a package of code that extends or customizes Adobe Commerce functionality without modifying the core platform. Modules can add new features, integrations, APIs, admin functionality, or frontend capabilities.

At a minimum, every Magento module requires registration.php, module.xml, and the appropriate directory structure. Depending on the functionality, additional files such as controllers, routes, dependency injection (di.xml), layouts, templates, and configuration files may also be required.

Editing core files makes future upgrades difficult and can introduce compatibility issues. Custom modules follow Adobe Commerce best practices by keeping your changes isolated, maintainable, and upgrade safe.

Yes. Custom modules can integrate with ERP, CRM, PIM, payment gateways, shipping providers, marketing platforms, and other third party applications using APIs, webhooks, or message queues.

Codilar develops custom Adobe Commerce modules, enterprise integrations, headless commerce solutions, performance optimizations, and migration services. Our certified developers build scalable, secure, and upgrade friendly solutions tailored to your business requirements.

CTA Background

eRetail Growth
in Mind?

Get tailored technology solutions to scale your retail business online

Request A QuoteArrow
CTA Background

Talk to Our
eCommerce
Expert

Book A MeetingArrow
Mail

Subscribe to
Stay in Know

Stay ahead with insights, trends, and brand success stories from the world of Digital Commerce.

Related Blogs

Loading...

Our Offices Are Here

Saudi Arabia flag

Saudi Arabia

Level 1, Building 7, Zone A Airport road, Business Gate P.O Box 93597 Riyadh 11683, KSA

+966 50 809 6356

UAE flag

UAE

DTECH, Techno Hub 1, Dubai Silicon Oasis Authority, United Arab Emirates - Dubai - United Arab Emirates

+971 55 557 8583

Oman flag

Oman

Building No 2/786, Way No 43, Block No 336, Al Khud 132, Muscat, Oman

+968 7694 6200

Singapore flag

Singapore

Codilar Digital Pte Ltd, 68 Circular Road, #02-01, 049422, Singapore

India flag

India

7th Floor, Jupiter Block Prestige Tech Park, Kadubeesanahalli, Bellandur Amtankere, Bengaluru, Karnataka 560103

+91 888 49 00 505

Indonesia flag

Indonesia

Satrio Tower, Floor 6, Unit C and D, Desa/Kelurahan Kuningan Timur, Kec Setiabudi, Kota Adm Jakarta Selatan, Provinsi DKI Jakarta

CONTACT US

Let's do something great together

We look forward to hearing from you

REGIONS

KSA|UAE|OMAN|INDIA|SINGAPORE|INDONESIA