Magento 2 Development Workshop Learn More

Magento 2 for Beginners

10 lessons

A Module in Magento 2

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.

Lesson Overview

In this lesson we are going to learn the following:

  • What is a Module in Magento 2?
  • How does a module name looks like?
  • Where a Magento 2 module can be located?
  • How to create a Magento 2 Module?
  • How to register a Module in a 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 is basically the same thing.

Magento 2 Modules

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 for the e-commerce 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 allow to extend 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 from one side and from 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 Magento vendor name and a Catalog module name, which is a Magento_Catalog or 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.

Magento 2 Modules

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, internal or in-house developers in order 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 lesson.

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/ModuleName directory location.

Let's create a vendor directory. In this lesson, the vendor is a MageMastery. Inside the app/code/MageMastery directory, let's create a module directory. For the module name, this is going to be a FirstModule directory name.

There are two required files that have to be created in each Magento 2 module. These files are used to notify to Magento 2 about a module. The registration.php and module.xml files.

Module Required 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 also used to provide a location to a Magento 2 module. Place the new registration.php file inside the app/code/MageMastery/FirstModule directory.

The content of the file is as follows:


use Magento\Framework\Component\ComponentRegistrar;


The Magento\Framework\Component\ComponentRegistrar class is used to register a new module in a Magento 2 application.

The static register() method accepts three arguments. The $componentType argument accepts a type of a component, which in case of a module is ComponentRegistrar::MODULE. The ComponentRegistrar::MODULE is a constant of the Magento\Framework\Component\ComponentRegistrar class 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/FirstModule in 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/ModuleName directory (in 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 ComponentRegistrar class 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 a main module's configuration file. It is required to place this file into the module's etc directory. Let's create the directory inside the app/code/MageMastery/FirstModule directory module.

An example of the XML configuration file is as follow:

<?xml version="1.0"?>


This is a 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:noNamespaceSchemaLocation attribute 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 it into XML files.

As a result, the final module.xml file should look like follow:

<?xml version="1.0"?>
<config xmlns:xsi="" 
    <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_First module.

bin/magento setup:upgrade

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.

grep MageMastery_FirstModule app/etc/config.php

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.

Next Steps

Subscribe to the Mage Mastery newsletter to hear first about new lessons and courses.