Create a simple module in Magento 2

      No Comments on Create a simple module in Magento 2

Magento is a complex e-commerce platform with a lot of features. It’s a bit difficult to understand it’s structure if you are developing a module for the first time. Though once you get to know it’s structure you will start loving it. I’ve been working on Magento 2 for some time and wanted to share how to create a simple module with frontend controller in Magento 2.

First step is to register our module (module/extension is called a component). I really liked the modularity of Magento 2 which allows us to develop our complete module at one place (app/code/vendor/module) rather than having our files (php, layout, templeate, skin) placed in different locations which was the case in Magento 1. So, let’s get started.

Create the module directory Amitshree/Helloworld in app/code directory where we will develop our module. So our module directory will be app/code/Amitshree/Helloworld

Following files will be used in this tutorial:
  • Amitshree/Helloworld/registration.php
  • Amitshree/Helloworld/etc/module.xml
  • Amitshree/Helloworld/etc/frontend/routes.xml
  • Amitshree/Helloworld/Controller/Index/Index.php

Create file registration.php inside our module which will register our component in Magento system with following code:


<?php

\Magento\Framework\Component\ComponentRegistrar::register(
Magento\Framework\Component\ComponentRegistrar::MODULE,
'Amitshree_Helloworld',
__DIR__
);

Now crete file etc/module.xml . This file is used for module declaration

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Amitshree_Helloworld" setup_version="2.0.0"/>
    </config>

Now to define our module whether it will be loaded on frontend or admin panel, we need to define route . Since we are going to develop our module on store front create file etc/frontend/routes.xml with following code


<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
        <router id="standard">
            <route id="helloworld" frontName="helloworld">
                <module name="Amitshree_Helloworld" />
            </route>
        </router>
    </config>

Where router id is standard which means this controller should be applied on storefront, route id should be a unique id and front name is defined in frontName keyword which is used to access your controller like website.com/helloworld/ .

Now create the controller file Controller/Index/Index.php with following code

<?php 
namespace Amitshree\Helloworld\Controller\Index; 
class Index extends \Magento\Framework\App\Action\Action 
{ 
    protected $resultPageFactory;
    
    public function __construct( 
        \Magento\Framework\App\Action\Context $context, 
        \Magento\Framework\View\Result\PageFactory $resultPageFactory 
    ) { 
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
      }
    public function execute()
    {
        echo "Hello World!";
    }
}

Magento 2’s url is created in following pattern website.com/<front_name>/<controller_folder_name>/<controller_class_name> . Where front_name will be defined in area specific router file.

When there is a request for url in above pattern it will automatically call execute() method defined in controller class.

Run php bin/magento setup:upgrade once to update the database schema. Now clear var/cache, var/page_cache and var/generation folders.

Check whether your module is enabled or not either

  • In magento backed at  Stores > Configuration > Advanced (dropdown) > Advanced
  • or By running command php bin/magento module:status (check status is enabled modules list)

Once it is confirmed that module is enabled  run the url website.com/helloworld/index/index in your browser and you should be able to see the output

Hello World!

Congrats,  you just configured your first Magento 2 module.

Leave a Reply