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 is basically used to extend its functionality.
In many cases, you may be required to add a customized functionality. So here’s the best approach for Magento 2: create module that meets your store requirements.
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 setup the structure of 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 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 module. This file must be placed in the root directory of module.
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 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 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 fist time so you have to upgrade database of module by using this command line:
php bin/magento setup:upgrade
After successfully enabling the module, check the status of modules again 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 custom tab on product page in Magento 2. If you have any problem or would like to add to the discussion, leave a comment below!