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.
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.

Note – (if the code folder is not available then please create it).
Then we have to create the module folder “BlogManager” (the module name).

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__ );

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>


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

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 till now

Know more about Magento 2.
Next blog -> Magento 2 Routing
Previous blog -> Magento 2 Area codes
Be the first to comment.