The Magento code is divided into individual modules. In MVC based PHP application, all the controllers and models have their separate folders. All files are combined together based on their functionalities, called Modules, in Magento and are primarily used to enhance the working of the application.
In many cases, you may be required to add a customized functionality. So here’s the best approach for Magento 2: create the module that meets your store requirements.
A Magento Module is a group of directories containing blocks, controllers, helpers, and models that are needed to create a specific store feature. It is the unit of customization in the Magento platform. Magento Modules can be created to perform multiple functions with supporting logic to influence user experience and storefront appearance. It has a life cycle that allows them to be installed, deleted, or disabled. From the perspective of both merchants and extension developers, modules are the central unit of the Magento platform.
Scalable, Super Fast, & Secure Magento Hosting
Our Clients Love us because we never compromise on these features
In this part of the tutorial series, we’ll discuss the topic: “Magento 2 Create Module” and cover the following areas:
- Structure of Module.
- Declaration of Module.
- Registration of Module.
- Check Module Status.
Structure of Module
In order to create a simple module in Magento 2, we first need to set up the structure of the module by creating folders as shown below:
Cloudways: namespace name.
Mymodule: module name.
Controller: Controls the flow of application execution.
Index: Controller name of the module and contains the action file.
Etc: Contains the configuration file of the module.
Etc/Frontend: Contains the router file.
View/Frontend: Contains Layout and Templates Folder.
View/Frontend/Layout: Contain XML file.
View/Frontend/Templates: Contain .phtml file.
Composer.json: Basically used for the updating of product edition like the module. This file must be placed in the root directory of the module.
Bonus Tip: On an average, only 2.5% visitors convert. You can increase this number by improving the page loading speed and layout. Get best hosting for Magento 2 stores for instant performance improvement.
Declaration of Module
We have to declare the module by using the configuration file. As Magento 2 search for configuration module in etc directory of the module. So now we will create configuration file module.xml.
The code will look like this:
<?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="Cloudways_Mymodule" setup_version="1.0.0"></module> </config>
<config> : Root node for configuration of Magento module.
<module>: Basic information about Magento module will be placed here.
name attribute in <module>: Namespace_Modulename.
Setup_version in <module>: Version of Magento module.
Registration of Module
The module must be registered in the Magento 2 system by using Magento Component Registrar class. Now we will create the file registration.php in the module root directory:
The Code will look like this:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Cloudways_Mymodule', __DIR__ );
Cloudways: Namespace name.
Mymodule: Module name.
Check Module Status
After following the steps above, we would have created a simple module. Now we are going to check the status of the module and whether it is enabled or disabled by using the following command line:
php bin/magento module:status
If you found your module in the disabled list, then you have to enable it by using the following command:
php bin/magento module:enable Cloudways_Mymodule
Now that you have enabled the module first time, so you have to upgrade the database of the module by using this command line:
php bin/magento setup:upgrade
After successfully enabling the module, recheck the status of modules with the command line that was mentioned before.
Finally, we have created a module in Magento 2. In the next part, we will learn how to add the custom tab on the product page in Magento 2. Leave a comment below if you have any problem or would like to add to the discussion!
Customer Review at
“Great speed, features, knowledgebase, dashboard, UX and fast, expert support. Very happy!”
Stefan [Management Consultant]
Syed Muneeb Ul Hasan is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. He is an expert in PHP and Magento and prefers to educate users in the implementation of Magento. When not working, he loves to play games and watch cricket.