cancel
Showing results for 
Search instead for 
Did you mean: 

[Video] How To Create A Module in Magento 2

sherrie
Community Manager

 

A module is a structural element of Magento 2. The whole system is built upon modules.

Typically, the first step in creating a customization is building a module.

 

To create a module, you need to complete the following steps:

 

  1. Create the module folder.
  2. Create the etc/module.xml file.
  3. Create the registration.php file.
  4. Run the “bin/magento setup:upgrade” script to install the new module.
  5. Check that the module is working.

 

Let’s go through each of these steps.

 

(1) There are two possible locations for modules in Magento 2: the app/code folder and the vendor 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 (for cloning GitHub).

 

Which of these locations should you choose for your new module?

If you build a module for a specific project, it is best to choose the app/code folder and commit to the project's repository.

 

If you build an extension to be reused, it is better to use Composer to create it, and put your module in the vendor/<YourVendor>/module folder.

 

Each module name in Magento 2 consists of two parts: the vendor and the module itself. In other words, modules are grouped into vendors, so you need to define the vendor and module names. For this example, let’s name the vendor “Learning” and the module “FirstUnit”.

 

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:

 

  1. cd to the root directory
  2. mkdirapp/code/Learning
  3. mkdirapp/code/Learning/FirstUnit

 

Make sure you have permission to create files and folders in your installation.

 

(2) Next, you need to create an etc/module.xml file. This file is required for the module to exist.

This file contains the following information:

 

  • Module name
  • Module version
  • Dependencies

 

Module name is defined by the folders we just created, because in Magento 2, class names must follow the folder structure. As we create the folders Learning/FirstUnit, our module name will be Learning_FirstUnit and all classes that belong to this module will begin with Learning_FirstUnit.

 

Module version indicates the current version of the database schema and data, and is used in upgrading.

 

Magento 2 has install and upgrade scripts in every module (optionally). To track whether to execute a script or not, Magento 2 uses module versions. Every time you implement a new database change, you implement a new version of a module and change the corresponding module.xml.

 

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. We will add our dependency via the <sequence>; enter in Magento_Catalog as our dependency.

 

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>

 

Note that in the XML file we specified:

 

  • Module name: Learning_FirstUnit (based on the folders we created)
  • Version: 0.0.1 (initial version of our module)
  • Dependency: Magento_Catalog

 

(3) Next, you need to create the registration.php file.
Each module must have this file, which tells Magento how to locate the module.

 

Continuing our example, 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__
);

 

The registration.php is a standardized file that follows the same pattern for all modules.

The only thing that varies is the module name, which in our case is Learning_FirstUnit

 

(4) The next step is to run the “setup:upgrade” command.

Running this command makes your new module active, notifying Magento of its presence.

 

php bin/magento setup:upgrade

 

It should echo a large amount of output, one line of which should be Learning_FirstUnit. Verify that this line of code is there.

 

(5) Finally, check that the new module is active.

So far, we haven't added any useful code to our module – it is still empty (and therefore invisible). In order to verify that it has been recognized, check the file app/etc/env.php. It has a list of auto-generated modules that are active.

 

Never change this list manually!

 

cat app/etc/env.php | grep Learning_FirstUnit

 

Employing these steps, you can successfully create a new module in Magento 2.

 

For more information on preparation and configuration in Magento 2, check out Unit 1 of the Fundamentals of Magento 2 Development (On-Demand) course.

About the Author