Magento 2 module is the structural element that makes up the code base of the whole system. Magento 2 Modules allow developers to add, modify, or enhance features without directly altering the core code of the Magento application. 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.
How To Create a Custom Module in Magento 2?
Step 1. Create the module folder.
Step 2. Create the etc/module.xml
file.
Step 3. Create the registration.php
file.
Step 4. Run the bin/magento setup:upgrade
script to install the new module.
Step 5. Check if the module is working.
Let’s go through all these steps in detail.
Step 1. 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 'MageDelight' and the module 'RocketBazaar'.
Now we'll create the folder app/code/MageDelight and in this folder, we'll place another folder: RocketBazaar.
To use the command line, the code would be:
cd
to the root foldermkdir app/code/MageDelight
mkdir app/code/MageDelight/RocketBazaar
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 MageDelight/RocketBazaar, the module name will be MageDelight_RocketBazaar and all the classes that belong to this module begin with MageDelight\RocketBazaar. For an instance, MageDelight\RocketBazaar\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/MageDelight/RocketBazaar/etc:
mkdir app/code/MageDelight/RocketBazaar/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="MageDelight_RocketBazaar" setup_version="0.0.1"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config>
Note that in the XML file we specified earlier:
- Module name:
MageDelight_RocketBazaar
(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.
Step 2. Create the etc/module.xml Declaration File
Once you’ve created the new module directory, you need to create the etc/module.xml
file to list the module name, version, and dependencies.
The module name is determined by the folders created in the previous step. Since we named the folders MageDelight/CustomModule
, our module will be called ‘MageDelight_CustomModule.’
The module version indicates the current version of the database schema and data. If you make changes to the table schema in your module, updating your module version will ensure it updates across all instances of your code in the Magento application.
We’ll list the initial version for our current module as 0.0.1.
The module dependencies list references any other modules your custom module depends on. We’ll make our simple custom module dependent on Magento’s core catalog module for this guide.
First, create the etc/
folder inside your module directory using the following command:
$ mkdir app/code/MageDelight/CustomModule/etc
Then, create the module.xml
file using the nano text editor as follows:
$ nano app/code/MageDelight/CustomModule/etc/module.xml
Finally, paste the following code into it:
<?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="MageDelight_CustomModule" setup_version="0.0.1"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config>
You can add more module dependencies by adding the <module name="..."/>
node inside the sequence node. Make sure to replace the ellipsis (...
) with the default values of your preferred Magento core modules.
Step 3. 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/MageDelight/RocketBazaar/registration.php
. Now, put the following content into it:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MageDelight_RocketBazaar', __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.
Step 4. 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 MageDelight_RocketBazaar
. Verify that this line of code is there
Step 5. 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 MageDelight_RocketBazaar app/etc/config.php
And here you go, these steps can lead you to successfully create a new custom module in Magento 2.
However, if you still need professional assistance with your Magento agency, feel free to reach out to us anytime.
Also read: How to Create EAV Module in Magento 2: Step-by-Step Tutorial?