Create your first module in Magento 2

ITdeation Technologies LLP, Nausif Maredia

New to Magento 2?

Here's the guide on “How to create your very first module in Magento 2?

Folder structure of our basic module.

Add code inside app/code/{{Vendor Name}}/{{Module Name}}

For example,


To get a list of modules pre-installed after installing Magento 2, run following command in terminal

php bin/magento module:status

To get the module listed, create following two files

  •  registration.php

  • module.xml

1. Create Registration

First, register the module by adding registration.php file here


Add following code in registration.php

 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.


2. Create Module xml

Add file module.xml here


Add following code to module.xml

<?xml version="1.0"?>
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Itdeation_HelloWorld" setup_version="0.0.1"> 

Now run following command again

php bin/magento module:status

Our module is now listed under disabled modules.

Run following command to enable it

php bin/magento module:enable Itdeation_HelloWorld

And then upgrade the module by running following command

php bin/magento setup:upgrade

3. Create a Router

Module is now enabled, let's print “Hello World” on front-end.

Define a router to hit a custom url.

Create routes.xml here


Add following code in routes.xml

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

Here we’re defining our router id as "standard" and route with an id “helloworld”.

The frontName attribute is going to be the first part of our URL.

In Magento 2 URL’s are formed as:


So in our example, the URL will look like this:


4. Create a Controller

Let's create Index.php here,

Add below code in Index.php


namespace Itdeation\HelloWorld\Controller\Index;

use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
    protected $_resultPageFactory;

    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
        $this->_resultPageFactory  =$resultPageFactory;

    public function execute()
        $resultPage = $this->_resultPageFactory->create();
        return $resultPage;

5. Create a Block

Create a block that will return "Hello World" string to be rendered on screen.

Create a HelloWorld.php file here

Add following code to HelloWorld.php

namespace Itdeation\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
    public function getHelloWorldString()
        return 'Hello World';

Create a phtml file to call the above block and get the Hello World string.

6. Create phtml file

Create view.phtml here


Add following code to view.phtml

<?php echo $block->getHelloWorldString(); ?>

7. Create a layout

Create a layout file to tell magento which controller to use and which view and block files to use on hitting url.

Create layout file helloworld_index_index.xml here


Add below code in helloworld_index_index.xml

<page xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
        <referenceContainer name="content">
            <block class="Itdeation\HelloWorld\Block\HelloWorld" name="helloworld" template="view.phtml" />

Clean the cache by running below command in terminal

php bin/magento cache:flush

Hit the following url in browser


Here's the output on frontend.

Voila! You have successfully created your first module in magento 2.