The module is basically a structural element of Magento 2. A whole new system is built upon modules in Magento 2. The first step towards building a customized eCommerce store is building a module.
In today’s tutorial, we will help you with a guide to create a custom module. Follow these steps to create a module:
- Create the module folder.
- Create the
etc/module.xml
file. - Create the
registration.php
file. - Run the
bin/magento setup:upgrade
script to install the new module. - Check if the module is working.
Let’s go through all these steps in detail.
CREATE THE MODULE FOLDER
In Magento 2, there might two possible folders where you can locate the folder. The app/code folder and the vendor folder. Based on how the Magento 2 has been installed, core modules can either be located vendor/magento/magento-*
folders (for composer installation) or in the app/code/Magento/
folder (for cloning GitHub).
Which One should you choose for the new Module?
If you are building a module for a specific project, it is preferable to select the app/code folder and commit to the repository of the project. But if you are building an extension to be reused, it is recommended to use composer to create it and put your module in vendor/<YOUR_VENDOR>/module-something folder.
In Magento 2, each module name consists of two parts – first is the vendor and second is the module itself. Basically, modules are grouped into vendors, so you are required to define the vendor and module names. For an example, lets name the vendor ‘Mageget’ and the module ‘Firstmodule’.
Now we’ll create the folder app/code/Mageget and in this folder, we’ll place another folder: Firstmodule.
To use the command line, the code would be:
cd
to the root foldermkdir app/code/Mageget
mkdir app/code/Mageget/Firstmodule
Note – Make sure you have permission to create files and folders in your installation
Now, you’d be required to create a etc/module.xml
file. The file is necessary for the module to exist.
The file contains information such as Module Name, Module Version and Dependencies. Let us discuss all these a bit more to understand better.
The module name is defined by the folders we just created. In Magento2, class names must follow the folder structure. Because we created the folders Mageget/Firstmoduler, the module name will be Mageget_Firstmodule and all the classes that belong to this module begin with Mageget\Firstmodule. For an instance, Mageget\Firstmodule\Observer\Test.
Module Version – The current version of the database schema and data is indicated by the Module version. For an instance, let’s assume you decide to modify a table’s schema in your module. But it can’t be sure that this change will happen on all the instances where the code is deployed? At this time, altering the database by direct SQL queries won’t work. Magento 2 offers to install and upgrade scripts in every module. The scripts contain commands in order to modify the database schema or data.
In order to track whether to execute a script, Magento 2 makes use of module versions. Every time you implement a new database change, you implement a new version of a module and change the corresponding module.xml
. Magento saves the current module’s version in a database and if the database value and the one in the module.xml
do not match, it will execute the upgrade code.
Dependencies. If one module depends on another, the module.xml
file will have a special declaration that defines a list of modules that the current module depends on. For this example, we will make our module dependent on Magento_Catalog.
With the following command-line code, create the folder app/code/Mageget/Firstmodule/etc:
mkdir app/code/Mageget/Firstmodule/etc
Now, put the following code in it,
<?xml version="1.0"?> <config xmlns:xsi=" " xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Mageget_Firstmodule" setup_version="0.0.1"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config>
NOTE THAT IN THE XML FILE WE SPECIFIED EARLIER:
- Module name:
Mageget_Firstmodule
(based on the folders we created) - Version: 0.0.1 (initial version of our module)
- Dependency: Magento_Catalog. We could have multiple dependencies. In this case, we would put
<module name=”..” />
nodes under the sequence node.
NOW, CREATE A REGISTRATION.PHP FILE
Each module must have this file, which guides Magento on how to locate the module. Continuing our example, create the file app/code/Mageget/Firstmodule/registration.php
. Now, put the following content into it:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Mageget_Firstmodule', __DIR__ );
This registration file is a standard file that follows the same pattern for all modules.
The module name is the only thing that varies.
RUN THE “SETUP: UPGRADE” COMMAND
This command makes your new module active along with notifying Magento of its presence.
php bin/magento setup:upgrade
It should echo a large amount of output, one line of which should be Mageget_Firstmodule
. Verify that this line of code is there
CHECK IF THE NEW MODULE IS ACTIVE
Until now, we haven’t added any useful code to our module – it is still empty. So, to verify that it has been recognized, check the file app/etc/config.php. It consists of a list of auto-generated modules that are active.
grep Mageget_Firstmodule app/etc/config.php