Hello Everyone! This is Mo. Here we explain in detail how to create a module and the necessary files to create a module in Magento2.
If you are new to PHP we recommend you read our blog on PHP Open-source (Free) Frameworks.
If you are new to Magento we recommend you read our blog Thing's need to know before you start development in Magento.
Before moving further we will let you know what is a module in Magento 2.
A module is a directory that contains the PHP, XML, JS, CSS, PHTML, JSON, and sometimes even CSV (translation purposes and other needed) files.
To get more insights about the Magento module click here.
Since Magento follows MVVM architecture you need to know the file structure and hierarchy before creating a module.
Below mentioned is the screenshot of the file structure.
The three necessary files to bootstrap a module are
1. registration.php
2. etc/Module.xml
3. composer.json
What are you thinking? Why do we need these files for creating?
What is the purpose/use of these files?
If this question has arisen in your mind then you are on the right path, we will explain to you why these files are needed. Magento 2 module-based architecture keeps the module files in one folder.
You can find Module in one of two places:
- app/code/VendorName/ModuleName
- vendor/vendor-name/module-name
With Magento 2, modules can be installed with Composer, which makes it easier for upgrades and version management.
These modules are placed in /vendor. Any file in /vendor is considered but not directly editable as those files will be removed when new versions are released.
Installing modules via composer into the /vendor folder will directly manage the versions of these modules and Composer does it for you.
In most cases, the modules that we develop will be in the app/code/VendorName.
This is the namespace used for development. Modules are placed in the folder directly below.
The only required files to initiate a module are.
- registration.php
- etc/module .xml
- registration.php
We need a registration.php file in every module. The reason is in each module is that it must be registered as a component of the Magento system through the Magento ComponentRegistrar class.
Now we will create the registration.php file in the app/code/VendorName/ModuleName/ location.
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, '<VendorName_ModuleName>',__DIR__);
So this will be the content of your registration.php file. This file is included by the Composer autoloader ( app/etc/NonComposerComponentRegistration.php). This adds the module to the static list of components in \Magento\Framework\Component\ComponentRegistrar. Once it is there, Magento will look for etc/module.xml.
The purpose of the module.xml file is to give a name to your component(Module) and a version for your module. Now let’s go with creating module.xml, So you have to create this file in app/code/VendorName/ModuleName/etc/
<?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="VendorName_ModuleName"/> </config>
This file specifies the setup version and loading sequence for the module. The setup version is available in the module’s Setup classes to determine what upgrade activities should occur.
The sequence tells Magento in which order to load modules. Located on the Loader.php file.
(vendor/magento/framework/Module/ModuleList/Loader.php).
This adjustment happens only when the module is installed. The list of modules is stored in app/etc/config.php
The sequence is useful for plugins and preferences and layouts. If we modify the layout of another module and your module is initialized first, the other module will overwrite your adjustments.
After doing all of these, open your terminal and navigate to your Magento root directory.
Type bin/magento setup: upgrade command, in your terminal, it adds your module in the database schema and it also clears the compiled code which resides in the generated/code folder and it also clears the cache.
If you don’t need then your compiled files need to be cleared
bin/magento setup:upgrade –keep-generated
Then to check your module status type proceed with the below comment.
bin/magento module:status
If you want to enable your module to proceed with the below-mentioned comment
bin/magento module:enable VendorName_ModuleName
If you want to disable your module proceed with the below-mentioned comment
bin/magento module:disable VendorName_ModuleName
Module Limitations: Magento 2 modules are to be completely contained within the module’s directory. All customizations and extensions are made within that folder.
If we disable the module, the module is not functional, Keep in mind that disabling a module does not automatically remove its tables or entries in the database.
Thank you for Reading. Are you fascinated to know more about Magento, What are you waiting for? Check out the other blogs in the Magento section and follow us through Youtube and Instagram to know more about us.
0 Comments