How to Create Module in Magento 2 Step By Step Guide
Table of Content
- Introduction
- Structure to Create Module in Magento 2
- Magento 2 Module Declaration
- Registration of Created Module in Magento 2
- Enable the Module in Magento 2
- Conclusion
Introduction
The Magento code is broken down into modules. All controllers and models in an MVC-based PHP application have their directories. In Magento Services, all files are bundled into Modules depending on their functionalities and are utilised mainly to improve the application’s functionality.
You may be required to create bespoke functionality in several circumstances. So, the ideal option is to design a Magento 2 module that best meets your store’s needs.
A Magento module is a collection of directories that contain the blocks, controllers, helpers, and models required to implement a specific store feature. It is the Magento platform’s customization unit.
According to merchants and extension developers alike, modules are the heart of the Magento platform. Magento modules can be built to execute various tasks and include logic to influence the user experience and storefront appearance. They can be installed, deleted, or disabled during their life cycle.
Create Module Folder
The approach for establishing a module in Magento 2 differs significantly from the previous version. Code pools have been removed from the code-base file structure. In the app/code folder, all modules are grouped by namespace. It implies you will have to do a module in app/code/Vendor>/ModuleName>.
The module folder and other necessary files must be created before the module can be used. This step will simplify registering your new module for the latest version of Magento.
Create these two folders:
app/code/webstore
app/code/Webstore/ModuleName
Note: The ‘Webstore’ is a module’s namespace, and the ‘ModuleName’ is the module’s name.
Magento 2 Module Declaration
After you have made your module folder, create a module.xml file in the app/code/Webstore/ModuleName/etc folder with the following code:
<?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=”webstore_HelloMagento” setup_version=”1.0.0″ />
</module>
</config>
It will generate a configuration in the module, etc. The directory will allow Magento 2 to quickly recognize the module’s name and version.
We will register a module with the name website ModuleName and version 1.0.0 in this file.
Registration of Created Module in Magento 2
In Magento 2, you will need to use the Magento ComponentRegistrar class to register your modules with the Magento system. As a result, use the following instructions to create a registration.php file in the app/code/Webstore/ModuleName folder (we are generating this file in the Magento root folder):
<?php
\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,’webstore_ModuleName’,__DIR__);
Enable the Module in Magento 2
You can enable your module in the Magento 2 environment after registering it. You will need to execute the following command line to see if Magento has recognized the new module:
php bin/Magento module:status1
The result of the preceding step is as follows:
List of disabled modules:
Webstore_ModuleName
It indicates that the system has detected the module, but it is not activated in your Magento system. To do so, use the following command to enable the module:
php bin/Magento module:enable Webstore_ModuleName
It will activate the module in your system and display the following result:
The following modules have been enabled:
– Webstore_ModuleName
Because you’re enabling this module for the first time in your system, use the following command to allow Magento to inspect and upgrade the module database easily:
php bin/Magento setup:upgrade
Note: Go to Admin » Stores » Configuration » Advanced » and click on it. Advanced to see if your new module is available.
Conclusion
That is it. If you carefully follow the methods mentioned above, creating a new module in Magento 2 might be an easy task. Using our source code, you can easily construct a top-notch module for your Magento web store.