magento 2 how to create custom module

Magento extension is a block of PHP code – a combine of functions, added into Magento to extend its functionality. 

Magento extensions allow you to add custom features and functionality to every area of your Magento store including the front and back end, integrations with other web services, marketing tools, themes and more. Extensions are developed through broad network of Magento partners to give you the flexibility and power to maintain your store the way you want . As you can see, if you want to create an extension , you have to have a lot of knowledge such as PHP, Mysql, Javascript, VPS … . Don’t worry about that. In this tutorial , I will introduce step by step How to Build Magento 2 Extension.

To create a Extension , you need to complete the following high-level steps:

  1. Create the module folder.
  2. Create the etc/module.xml file.
  3. Create the registration.php file.
  4. Run the bin/magento setup:upgrade script to install the new module.
  5. Check that the module is working

Step 1 : Create module folder in app/code/ You can see structure extension on Screen image

Let’s go through each of these steps in detail.

All extension code is place in app/code/VendorName/ModuleName . Example app/codeMagebay/Hello/

Step 2: create an etc/module.xml file. This file is required for the module to exist.

File : app/code/YourVendor/YourModule/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Magebay_Hello" setup_version="2.1.5" active="true">
              <module name="Magento_Catalog"/>

Note that in the XML file we specified:

  • Module name: Magebay_Hello (based on the folders we created) Module name is defined by the folders we just created, because in Magento 2, class names must follow the folder structure. For example: Magebay\Hello\Controller\Index
  • Version: 2.1.5 (initial version of our module)
    Module version indicates the current version of the database schema and data, and is used in upgrading, For example , if you want to add new table or new fields in table, You will increase version in this file and version in file Setup/UpgradeSchema.php . I will explain more in next step.
  • Dependency: Magento_Catalog. We could have multiple dependencies. In this case, we would put nodes under the sequence node.

Step 3; Create registration.php file

You have yo create file registration.php file app/code/YourVendor/YourModule/registration.php


Step 4 : Run php bin/magento setup:upgrade to install extension.

Step 5 : Check Module working

You have done simple module , You can create new controller to test.

Configure Router. add etc/frontend/routers.xml


<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="hello" frontName="hello">
            <module name="Magebay_Hello" />

File : app/code/YourVendor/YourModule/Controller/Index/Index.php
app/code/Magebay/HelloWorld /Controller/Index/Index.php

namespace Magebay\Hello\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
        $this->resultPageFactory = $resultPageFactory;
    public function execute()
		echo "module install";

Thank For reading The post, I will introduce how to add database and get it to frontend in next post, if you have any questions about How to Build Magento 2 Extension , you can comment under post so I will check and reply to you.

2 thoughts on “magento 2 how to create custom module”

Leave a Reply

Your email address will not be published. Required fields are marked *