The module is a structural element of Magento 2 – the whole system is built upon modules. Typically, the first step in creating customization is building a module.
To create a module, you need to complete the following high-level steps:
- Create a module folder.
Depending on how Magento 2 has been installed, core modules can either be located in the vendor/magento/magento-*folders (for composer installation) or in the app/code/Magento/ folder
Let’s create the folder app/code/Learning and inside this folder place another folder: FirstUnit. If you’re using the command line, the code would be:
- cd to the root folder
- mkdir app/code/Learning
- mkdir app/code/Learning/FirstUnit
2. Create the etc/module.xml file.
This file contains the following information:
- Module name
- Module version
- Dependencies
Using the following command-line code, create the folder app/code/Learning/FirstUnit/etc:
- mkdir app/code/Learning/FirstUnit/etc
Then put 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="Learning_FirstUnit" setup_version="0.0.1"> <sequence>
- <module name="Magento_Catalog"/> </sequence>
- </module>
- </config>
3. Create the registration.php file.
create the file
app/code/Learning/FirstUnit/registration.php
. Then put the following content into it:
- <?php \Magento\Framework\Component\ComponentRegistrar::register(
- \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',
- __DIR__
- );
4. Run the bin/Magento setup:upgrade script to install the new module.
- php bin/magento setup:upgrade
5. Check that the module is working.
- grep Learning_FirstUnit app/etc/config.php
These are just the basic steps that you need to follow for creating a module in Magento 2. Although, if you are a beginner then you need to understand and study the process beforehand to avoid errors. If you are looking for guidance then consulting a company providing Magento 2 development services is a good idea.