Magento 2 'Hello World' Module

Magento 2 'Hello World' Module
If you had already been into Magento development, you must know that there are various changes enforced in Magento 2, especially the folder structure. The most seen code pools are now gone forever. First, let’s begin with adding the module.xml file which would get our module initiated. Add module.xml file in the below path with following code:
Path: app/code/Velanapps/Tutorial/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="..
 <module name="Velanapps_Tutorial" setup_version="1.0.0">
Now that our module is initiated, we will need to register it using the registration.php.
Path: app/code/Velanapps/Tutorial/registration.php
Once our module is initiated and registration process is completed, let’s create a controller file. In Magento 2, you need to create a file for the action and not for a controller which was done for Magento 1.9 versions.
Path: app/code/Velanapps/Tutorial/Controller/Index/Index.php
namespace Velanapps\Tutorial\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Framework\App\Action\Action
 public function execute()
In Magento 1.9, we would specify our router in the config.xml file within the <router> tag. Well, in Magento 2, we would need to create a separate configuration file for routers:
Path: app/code/Velanapps/Tutorial/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="..
 <router id="standard">
 <route id="tutorial" frontName="tutorial">
 <module name="Velanapps_Tutorial" />
After the controller and routers are created, let’s look for the output that needs to get displayed in the browser. Let’s get started by creating the template file:
 Path: app/code/Velanapps/Tutorial/view/frontend/templates/index.phtml
<h1><?php echo __('Hello World!!!') ?></h1>
And finally, we need to create a layout xml file tutorial_index_index.xml in the following path and code:
 Path: app/code/Velanapps/Tutorial/view/frontend/layout/tutorial_index_index.xml
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="" layout="3columns" 
 <referenceContainer name="content">
 <block class="Magento\Framework\View\Element\Template" name="velanapps_tutorial_block"
 template="Velanapps_Tutorial::index.phtml" />
Now that all the coding stuffs are completed, we will need to run some commands in the command prompt to get our module up and running. If you use Windows machine then open cmd. In case of Linux machines, you will have to do this in the terminal:
<switch to your wamp server location C or D drive> (i.e)
cd wamp
cd www
cd <your magento folder name>
cd bin
php magento setup:upgrade
In the command prompt or terminal, you will see your module getting listed like below:
Module ‘Velanapps_Tutorial’;
The final step is to run our controller action in the browser:<folder_name>/tutorial/index/index/ 1 Click Here to download the Source Code.


  • On June 17, 2017 Siva wrote:
    Really Useful article
    Thanks for the share

1 Item(s)

Write Your Comment

Only registered users can write comments. Please, log in or register