
Creating a Simple “Hello World” Module in Magento 2
A step-by-step guide to building your first Magento 2 module from the ground up.
1. Initial Module Setup
Every Magento 2 module requires a specific file structure. First, you need to create the main directory for your module under app/code. This is where all your module’s files will reside.
Directory Structure:
app/code/MyVendor/MyModule
etc
registration.php
registration.php
This file is essential for Magento to recognize your module. It tells Magento where to find your module and what its name is. Create this file at app/code/MyVendor/MyModule/registration.php.
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'MyVendor_MyModule',
DIR
);
etc/module.xml
This XML file defines your module’s name, version, and its dependencies. Create this file at app/code/MyVendor/MyModule/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="MyVendor_MyModule" setup_version="1.0.0">
</module>
</config>
2. Enable the Module
After creating the core files, you need to tell Magento to enable your module. Open your terminal and run the following commands from your Magento root directory.
php bin/magento setup:upgrade
This command will register the module and update the database. Next, clear the cache to ensure the changes are live.
php bin/magento cache:clean
You can verify your module is enabled by checking the app/etc/config.php file. You should see ‘MyVendor_MyModule’ => 1, in the array.
3. Create a Simple Controller
A controller is the entry point for a web request. It handles the logic for a specific URL and returns a result. Let’s create a simple controller that will display “Hello World”.
Updated Directory Structure:
app/code/MyVendor/MyModule
Controller
Index
Index.php
etc
frontend
routes.xml
registration.php
etc/frontend/routes.xml
This file defines the front-end route for your module. It maps a URL to a controller. Create this file at app/code/MyVendor/MyModule/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="mymodule" frontName="mymodule">
<module name="MyVendor_MyModule" />
</route>
</router>
</config>
The frontName “mymodule” will be the first part of your URL (e.g., https://yourdomain.com/mymodule/index/index).
Controller/Index/Index.php
This is the controller class. It contains the logic to execute when the route is accessed. Create this file at app/code/MyVendor/MyModule/Controller/Index/Index.php.
<?php
namespace MyVendor\MyModule\Controller\Index;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
class Index implements HttpGetActionInterface
{
/**
* @var \Magento\Framework\Controller\Result\RawFactory
*/
protected $resultRawFactory;
/**
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
*/
public function __construct(
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
) {
$this->resultRawFactory = $resultRawFactory;
}
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
$resultRaw->setContents('Hello World!');
return $resultRaw;
}
}
With this file in place, you can now visit https://yourdomain.com/mymodule/index/index in your browser and it will display “Hello World!”. The two “Index” parts of the URL correspond to the Index directory and the Index.php file.
4. Run the Upgrade Command Again
To ensure your new routes are recognized by the system, run the setup:upgrade command one more time. This is a good practice whenever you add a new route, controller, or layout file.
php bin/magento setup:upgrade && php bin/magento cache:clean