Magento 2 'Hello World' Module

Hi folks. Our previous post was about configuring the WAMP server and installing Magento 2. That’s all what is needed for a basic user. For a developer who is striving to get their hands dirty on Magento coding, the rudimentary step would be to develop a 'Hello World' module. Yes, today we are going to dive into the coding part and say hello to the world in Magento 2 style.

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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="..
/../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
 <module name="Velanapps_Tutorial" setup_version="1.0.0">
 </module>
</config>
Now that our module is initiated, we will need to register it using the registration.php.
Path: app/code/Velanapps/Tutorial/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Velanapps_Tutorial',
 __DIR__
);
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
<?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()
 {
 $this->_view->loadLayout();
 $this->_view->getLayout()->initMessages();
 $this->_view->renderLayout();
 }
}
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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="..
/../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
 <router id="standard">
 <route id="tutorial" frontName="tutorial">
 <module name="Velanapps_Tutorial" />
 </route>
 </router>
</config>
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="http://www.w3.org/2001/XMLSchema-instance" layout="3columns" 
xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/
etc/page_configuration.xsd">
 <body>
 <referenceContainer name="content">
 <block class="Magento\Framework\View\Element\Template" name="velanapps_tutorial_block"
 template="Velanapps_Tutorial::index.phtml" />
 </referenceContainer>
 </body>
</page>
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)
D:
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: http://127.0.0.1/<folder_name>/tutorial/index/index/   1   Click Here to download the Source Code.
 

Comments

  • 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