Back to Top

Create Custom Module in Magento 2

Updated 12 March 2025

Creating a custom module in Magento 2 involves several structured steps to ensure that the module is properly defined, registered, and activated.

In Magento 2 modules are a standalone unit where we write our functionalities or business logic to achieve our goal.

It is a group of directories that contain the blocks, controllers, helpers, and models needed to create a specific store feature. It is the unit of customization in the Magento 2 platform.

You can create Magento 2 modules to perform various functions, such as influencing user experience and changing the storefront appearance. They can install, deleted, or disabled.

Here’s a detailed guide on how to create a simple custom module.

Searching for an experienced
Magento 2 Company ?
Find out More

Steps to Create a Module :

  • Create the module folder.
  • Create the registration.php file.
  • Create the module.xml file.
  • Run the command: php bin/magento setup:upgrade
  • Run the command: php bin/magento setup:di:compile

Create the module Folder :

To create the module we have to create a vendor folder first with the vendor name (which is Webkul in our case) under the app/code folder.

Folder structure

Note – (if the code folder is not available then please create it).

Then we have to create the module folder “BlogManager” (the module name).

Folder structure
Folder Structure

Now our folder structure should be like (app/code/Webkul/BlogManager). Where Webkul is our Vendor name and BlogManager is our Module name.

Create the registration.php file :

After this, we have to create a registration.php file under the file path (app/code/Webkul/BlogManager) so that Magento 2 can recognize it as a module and register it.

Code for app/code/Webkul/BlogManager/registration.php file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Webkul_BlogManager',
    __DIR__
);
Folder structure
Folder structure

Create the module.xml file. :

To create the module.xml file, we first need to create an etc folder under the directory (app/code/Webkul/Blogmanager), and in the etc folder we need to create a module.xml file with the following content.

Code for etc/module.xml file

<?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="Webkul_BlogManager">
    </module>
</config>
Folder structure
Folder structure
Magento Root Directory Path
For better understanding

Run the command: php bin/magento setup:upgrade

After creating these two files we need to run the setup upgrade command like below in the terminal at the Magento 2 root folder. It will activate the module.

php bin/magento setup:upgrade
Command Execution

This command is used to update the Magento 2 database schema and data, as well as apply any necessary system upgrades.

It is used in the Magento 2 upgrade process and ensures that your Magento 2 store runs smoothly with the latest updates and features.

Run the command: php bin/magento setup:di:compile

It is used to compile code like Factories, Proxies, Interceptors, etc., and puts them in the generated directory.

php bin/magento setup:di:compile
Folder Structure

Folder Structure till now

Folder Structure
Folder Structure

Know more about Magento 2.

Next blog -> Magento 2 Routing

Previous blog -> Magento 2 Area codes

. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home