How to create a simple Hello World module in Magento 2?

Create a Simple Hello World Module using Magento 2 is the Entry point for Learning a Magento 2.

Simple Module to display, Hello World in the browser is used for Beginner of Magento Developer who wants to explore Magento 2 in depth.

You must need to create two entry point file registration.php and module.xml file to define the Custom Module.

  • registration.php — Used to Register New Module In Magento 2 System.
  • module.xml — XML file defines our module name, Also contains <sequence> tag for Dependency of Other modules in our module.

The module is simply residing in the app/code folder of Magento instance.

Just Create Folder under the app/code,
For example, We kept Packagename as Rbj and Modulename as Training.
You can keep any name for Packagename/Modulename. In our demo, we have kept Packagename_Modulename as Rbj_Training.

Now start with basic module creation, Registration.php file is to register our module.  Create registration.php file,

Full Path,  app/code/Rbj/Training/registration.php,


Create module.xml file,

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:Magento:framework:Module/etc/module.xsd">
    <module name="Rbj_Training" setup_version="1.0.0"/>

In above file keep name attribute equal to your {Packagename_Modulename}

Now create routes.xml file,

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
	<router id="standard">
		<route frontName="training" id="training">
			<module name="Rbj_Training"/>

The frontName attribute is the first part of our custom URL. Default URL structure for Magento 2 like,  <frontName>/<areaControllerFolderName>/<controllerClass>

Where areaControllerFolderName is your area’s controller Folder Name.
An area might be front or admin area. In our case, <areaControllerFolderName> equals Index and   <controllerClass> equals Training for our Module.

Create <controllerClass>,


namespace Rbj\Training\Controller\Index;

class Training extends \Magento\Framework\App\Action\Action

    protected $resultPageFactory;

     * Constructor
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;

     * Execute view action
     * @return \Magento\Framework\Controller\ResultInterface
    public function execute()
        $resultPageFactory = $this->resultPageFactory->create();
        // Add page title
        $resultPageFactory->getConfig()->getTitle()->set(__('Training Module'));

        return $this->resultPageFactory->create();

In Magento 2 Controller file has execute() method, Its execute when our controller is called.  Now our final URL will look like this, training/index/training

Creating a layout and template files for our module. First, create a training_index_training.xml file in the app/code/Rbj/Training/view/frontend/layout folder with the following code:

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
		<referenceContainer name="content">
			<block class="Rbj\Training\Block\Index\Training" name="" template="Rbj_Training::index/training.phtml"/>

In the above content, Block class is Rbj\Training\Block\Index\Training, We need to define Block for keeping our templates dynamic function. We will create a block further in this tutorial.

Create template file training.phtml for our module representation in frontend,


/** @var $block \Rbj\Training\Block\Index\Training */
echo $block->getTextMessage();

<h2>Hello First Magento 2 Basic module.</h2>

Create Training.php block class for a dynamic function which is required to call in our template file.
The file path, app/code/Rbj/Training/Block/Index/Training.php


namespace Rbj\Training\Block\Index;

class Training extends \Magento\Framework\View\Element\Template

     * Constructor
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);

    public function getTextMessage()
        return 'Greetings!!! Magento 2 Module completed.';

Now When you hit the URL, {{Site_Base_Url}}/training/index/training,
You will redirect to the 404 pages because our module is not installed yet.

For installing our module in Magento 2,  We need to run the command using SSH,  Login with SSH and go to your project root path where Magento is installed,
Run below command,

php Magento setup:upgrade
php Magento setup:static-content:deploy -f
php Magento cache:flush

Now You run the URL in the frontend,
You have a display message which you have typed in the template file.

Result is,

create a simple Hello World module in Magento 2

Using the above steps, You can create your own Custom Module in Magento 2 with easy steps.