A Module in Magento 2
Magento 2 is widely known for its extensibility. One of the extensibility concepts which Magento 2 Open Source supports is called Modularity. In this lesson, we will learn what is a Magento 2 Module.
Today we want to share with you:
- What is a Module in Magento 2?
- What does a module name look like?
- Where a Magento 2 module can be located?
- How to create a Magento 2 Module?
- How to register a Module in Magento 2?
What is a Magento 2 Module?
A Module in Magento 2 is an independent component or a set of components that provide business logic and features. Magento 2 Open Source is a modular application and it supports modularity. It means that all functionality is implemented and delivered in components that are known as Modules. A Component is also known and called an Extension. A module and an extension are the same things.
Each extension or module in Magento 2 provides a set of capabilities that support the business and functional logic of a module. It allows bringing additional features to the eCommerce store which is based on Magento 2.
Let's take a Catalog module for example. The Catalog module provides a different set of features which include Category Management, Product Management, Product Types support for Simple and Virtual products, Catalog browsing capability, and so on.
Also, the module provides extensibility points for other modules which allows extending the Catalog module with additional functionality. An example of such an additional module is a Downloadable Product module.
A module in Magento 2 could bring a complex set of features on one side and on the other side, it could be a tiny feature that adds an icon to a Product page for example.
A Module Name
Each module's name consists of two parts. A vendor name and the name of a module. These two parts are concatenated with a _(underscore) or a \(backslash). As a result, the module name should follow the pattern VendorName_ModuleName. For example, a Catalog module name is going to be the name of a Magentovendor name, and a Catalogmodule name, which is a Magento_Catalogor Magento\Catalog.
Where should a Module be located in Magento 2?
That's a very interesting question. There are two locations where a module can be located in a Magento 2 application.
The first location is an app/code directory. This directory is used to add all custom and 3rd-party Magento 2 modules. This location is usually used by development agencies, and internal or in-house developers to simplify a development process.
The second location is a vendor directory. This directory is used during the composer package management installation. A module installation in Magento 2 with a composer package manager is the recommended way of installing a Magento 2 module.
Learn more about Magento 2 Open Source directory structure in the Project Structure Overview.
How to create a Module?
The first thing we have to do in order to create a module is to create a module directory. Usually, all modules are located under the app/code directory. If you follow the previous lesson, the code directory should be missing under the app directory of a Magento 2 project.
Inside the app directory, let's create the code directory. This is a directory where all modules should be located. Every Magento 2 module should be located under the app/code/VendorName/ModuleNamedirectory location.
Let's create a vendor directory. In this lesson, the vendor is a MageMastery. Inside the app/code/MageMasterydirectory, let's create a module directory. For the module name, this is going to be a FirstModuledirectory name.
There are two required files that have to be created in each Magento 2 module. These files are used to notify Magento 2 about a module. These are registration.php and module.xml files.
The registration.php file
The registration.php file is used to notify to Magento 2 application about a module in a file system. It is also used to provide a location for a Magento 2 module. Place the new registration.php file inside the app/code/MageMastery/FirstModuledirectory.
The content of the file is as follows:
The Magento\Framework\Component\ComponentRegistrarclass is used to register a new module in a Magento 2 application.
The static register()method accepts three arguments. The $componentTypeargument accepts a type of component, which in the case of a module is ComponentRegistrar::MODULE. The ComponentRegistrar::MODULEis a constant of the Magento\Framework\Component\ComponentRegistrarclass and it is recommended to use this constant for the module registration.
The second argument of the register()method is the name of the module. As you can see the name is a VendorName_ModuleName. In the above code sample, the name is MageMastery_FirstModule.
The final argument is a PHP __DIR__constant. It provides an actual location of the module and is interpreted into an /var/www/magento/app/code/MageMastery/FirstModulein case a Magento 2 application is installed in the /var/www/magento directory.
How does this work?
One of the allowed locations of the registration.php file is app/code/VendorName/ModuleNamedirectory (in the case of the current lesson the location is app/code/MageMastery/FirstModule/). The registration.php file is scanned and executed during the Magento 2 Open Source application start. The method register()of the ComponentRegistrarclass adds the module name and its location into the global pool of all module's locations. Once added, Magento 2 Open Source then goes through all registered modules paths and reads the configuration of the module.xml file.
That is why it is important to have both files to successfully register a Module in Magento 2.
The module.xml file
The module.xml file is used to declare and provide information about the module name and any dependencies to other 3rd-party or out-of-box Magento 2 modules.
The module.xml file is the main module's configuration file. It is required to place this file into the module etc directory. Let's create the directory inside the app/code/MageMastery/FirstModuledirectory module.
An example of the XML configuration file is as follows:
This is basic file content that is used across almost all Magento 2 configuration files. We are going to learn more about XML configuration files in the next lessons.
Additionally, all XML files in Magento 2 contain 2 attributes in the <config>node. The xmlns attribute provides a location to the XMLSchema instance and the xsi:noNamespaceSchemaLocationattribute provides a path to an XSD (XML Schema Definition) file of a Magento 2 framework. The last one is used to provide XML validation and syntax highlighting capability to a configuration file.
These attributes are not required, however, it is recommended to follow the standard Magento practices and include them in XML files.
As a result, the final module.xml file should look like the following:
<module name="MageMastery_FirstModule" />
The only required XML node which has to be added is a <module />one. The name attribute should include the name of the current module, which is MageMastery_FirstModule.
How to register a Module?
The last step is to run the command which will enable and install the MageMastery_Firstmodule.
You may notice a new module added to the list of modules.
In order to check if the module has been added and enabled in a Magento 2 application. Here I use a grep command to check a string in an app/etc/config.php file.
As a result, a terminal screen should show a
The value 1 means that the module is enabled, and 0 means that the module is disabled. As we can see from the output, the MageMastery_FirstModule is set to 1, which means that the module has been successfully registered in a Magento 2 Open Source application.
Want to learn more about Magento 2?
Join the FREE course Magento 2 for Beginners! This course is for developers who want to start developing with Magento 2. The course covers Magento 2 concepts and the architecture of the Magento 2 platform.