Magento, Magento 2

How to create custom module in Magento 2

The Magento module is a structural element that handles the flow of functionality. We can use a module for third-party module customization, override default Magento functionality, and implement new features. There are controller, block, model, etc, views directories to manage the business feature.
There are two directories where you can place your modules, app/code, and vendor. If you are building a module for a specific project, you can place your module in the app/code directory (cloning GitHub repo). And if you are building any reusable extension you can place your module in the vendor directory (composer installation).

Create module in app/code directory

Step 1: Create a module directory

In Magento2, the module name is defined in this way VendorName_ModuleName where VendorName is a group of modules and ModuleName is a module itself. So you need to create a module directory in this format: app/code/Vendor/Module. For example, app/code/Mage2/Demo where Mage2 would be your vendor and Demo would be your module.

Step 2: Create a registration.php file

Magento uses this file to understand the modules that are available for use. You need to create the registration.php file in the module’s root directory this way: app/code/Mage2/Demo/registration.php

<?php

declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Mage2_Demo', __DIR__);
Step 3: Create a module.xml file

The file is necessary for the module to exist. You need to create a module.xml file inside the etc directory this way: app/code/Mage2/Demo/etc/module.xml. The module.xml file contains the module name, module version, and dependent modules. The module name must be the same as you defined in registration.php. If you are using Declarative Schema for database management, please skip the setup_version attribute. The setup_version attribute is used to keep track of your database schema version.

<?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="Mage2_Demo" setup_version="1.0.0">
    </module>
</config>

You can define modules on which your module is dependent inside <sequence> element like follows:

<?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="Mage2_Demo" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>
Step 4: Create a composer.json file (optional)

composer.json file is required when you are creating a module in the vendor directory. Create a composer.json file in the module’s root directory this way: app/code/Mage2/Demo/composer.json

{
    "name": "mage2/module-demo",
    "description": "Mage2 Demo Extension",
    "require": {
        "php": "~7.4.0||~8.1.0"
    },
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
        "BSD-3-Clause"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Mage2\\Demo\\": ""
        }
    }
}
Step 5: Enable the module and check if it’s working

Now run the following commands in the command prompt:

bin/magento module:enable Mage2_Demo
bin/magento setup:upgrade

Now open the app/etc/config.phpfile, and search for Mage2_Demo. If you found your module name it means your module has been installed successfully.

Create module in vendor directory

Step 1: Create a module directory

Create a custom directory in Magento’s root directory. For example, <magento_root>/local-src. Create module directory as VendorName_ModuleName in that custom directory this way: <magento_root>/local-src/mage2_demo.

Step 2: Add module in Magento’s composer.json file

Run the following command to add your module in the repositories sections of Magento’s composer.json.

composer config repositories.mage2_demo path ./local-src/mage2_demo
Step 3: Create a composer.json file

This file is used by Composer which defines requirements and paths. You need to add a composer.json file to your module.

{
    "name": "mage2/module-demo",
    "description": "Mage2 Demo Extension",
    "require": {
        "php": "~7.4.0||~8.1.0"
    },
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
        "BSD-3-Clause"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Mage2\\Demo\\": ""
        }
    }
}
Step 4: Install the module

Now you need to install a custom module using composer.

composer require mage2/module-demo

The above command will install your custom module inside the vendor directory.

Step 5: Enable the module and check if it’s working

Now run the following commands in the command prompt:

bin/magento module:enable Mage2_Demo
bin/magento setup:upgrade

Now open app/etc/config.phpfile, and search for Mage2_Demo. If you found your module name it means your module has been installed successfully.

We hope this blog may understandable and useful to you. You can email us at mage2developer@gmail.com if we missed anything or want to add any suggestions. We will respond to you as soon as possible. Happy to help 🙂