Magento 2 Hello World Module Tutorial
In this tutorial, we will create a simple Magento 2 module called HelloWorld.
This module will display a basic Hello World message on the frontend.
Final Module Output
After completing this tutorial, you can open:
https://yourdomain.com/helloworld/index/index
And you will see:
Hello World from Magento 2 Module
Chapter 1: Module Folder Structure
Create this folder structure inside Magento root:
app/code/Acesoftech/HelloWorld/
│
├── registration.php
│
├── etc
│ ├── module.xml
│ └── frontend
│ └── routes.xml
│
└── Controller
└── Index
└── Index.php
Chapter 2: registration.php
Location: app/code/Acesoftech/HelloWorld/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Acesoftech_HelloWorld',
__DIR__
);
Chapter 3: module.xml
Location: app/code/Acesoftech/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="Acesoftech_HelloWorld" setup_version="1.0.0" />
</config>
Chapter 4: Frontend Route
Location: app/code/Acesoftech/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="Acesoftech_HelloWorld" />
</route>
</router>
</config>
Chapter 5: Controller File
Location: app/code/Acesoftech/HelloWorld/Controller/Index/Index.php
<?php
namespace Acesoftech\HelloWorld\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
class Index extends Action
{
public function __construct(Context $context)
{
parent::__construct($context);
}
public function execute()
{
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$result->setContents('Hello World from Magento 2 Module');
return $result;
}
}
Chapter 6: Run Magento Commands
Run these commands from Magento root folder:
php bin/magento module:enable Acesoftech_HelloWorld
php bin/magento setup:upgrade
php bin/magento cache:flush
If your Magento is in production mode, also run:
php bin/magento setup:di:compile
php bin/magento static-content:deploy -f
php bin/magento cache:flush
Chapter 7: Test the Module
Open this URL in browser:
https://yourdomain.com/helloworld/index/index
You should see:
Hello World from Magento 2 Module
Chapter 8: Troubleshooting
Check Module Status
php bin/magento module:status Acesoftech_HelloWorld
Clear Cache
php bin/magento cache:clean
php bin/magento cache:flush
Check Logs
tail -f var/log/system.log
tail -f var/log/exception.log
Conclusion
In this tutorial, we created a simple Magento 2 Hello World module using:
- Module registration file
- Module configuration file
- Frontend route file
- Frontend controller file
This is the basic foundation of Magento 2 custom module development.
Happy Coding!
