In this post we will create a simple Helloworld module in Magento 2. Fisrt of all you should know that in magento 2 there are no more code pools.Modules are grouped by namespace and placed directly in the app/code folder. Now let’s start!

Here is folders we will nedd to create this Module.

Step 1 : Open your app folder and create the following folders

If you don’t have code folder in your app directory, create it manually.

  • app/code/Webcreta
  • app/code/Webcreta/Helloworld

Here Webcreta is your namespace and Helloworld is you module’s name

Step 2 : Create a module.xml file in the app/code/Webcreta/Helloworld/etc folder with the following code:

<?xml version="1.0"?>

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Webcreta_Helloworld" setup_version="1.0.0">

Step 3 : To register our module, create a registration.php file in the app/code/Webcreta/Helloworld folder with the following code:



Step 4 : Open cmd and go to the Magento 2 root. Run this command:

php bin/magento setup:upgrade

Let’s make sure that our module is installed,To check that you can go to Admin → Stores → Configuration → Advanced → Advanced and check that the module is present in the list or you can open app/etc/config.php and check the array for the ‘Webcreta_Helloworld’ key, whose value should be set to 1.

Step 5 : To create controller first we need to create routes.xml file in the app/code/Webcreta/Helloworld/etc/frontend folder with the following code:

<?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="Webcreta_Helloworld" />

Here you can see frontName=”helloworld” so “helloworld” is going to be the first part of our URL.

Step 6 : Create the Index.php controller file in the app/code/Webcreta/Helloworld/Controller/Index folder with the following code:

<?php namespace Webcreta\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;

In Magento 2 every action has its own class which implements the execute() method.

Step 7 : Create a Helloworld.php file in the app/code/Webcreta/Helloworld/Block folder with the following code:

namespace Webcreta\Helloworld\Block;

class Helloworld extends \Magento\Framework\View\Element\Template
public function getHelloWorldTxt()
return 'Hello world!';

Next we are going to create layout and template files
In Magento 2, layout files and templates are placed in the view folder inside your module. Inside the view folder, we can have three subfolders: adminhtml, base and frontend.
The adminhtml folder is used for admin, the frontend folder is used for frontend and the base folder is used for both, admin and frontend files.

Step 8 : Create helloworld_index_index.xml file in the app/code/Webcreta/Helloworld/view/frontend/layout folder with the following code:

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

Here you can see template=”helloworld.phtml” so now we will create that file.

Step 9 : Create a helloworld.phtml file in the app/code/Webcreta/Helloworld/view/frontend/templates folder with the following code:

<h1><?php echo $this->getHelloWorldTxt(); ?></h1>

Ok we are done. Now open the http://localhost/magento/helloworld URL in your browser.

I Hope it helps!

Leave a Reply