
How to create a module in Magento2
Structure:
1. What is the module and why are we using it?
2. Where are Magento2 modules located?
3. Creating the first module and describing its structure
4. How to check that the module is working?
We are going to create the Magento2 module. You will find the information about the required files for your module here. I’ll explain in details what each file and directory does. We enable and disable the module in the Magento2 system.
I have prepared module, that will be described below, you can download it from here or write everything step by step by your own.
Let’s go.
1) What is the module and why are we using it?
The module is a directory where all code base is located (controllers, blocks, configurations, models, etc).
This code is responsible for some specific functionality and business logic. Best practice says that module should be self-sufficient. That means to have minimum relations to other modules or systems (but everybody knows that sometimes it is impossible).
2) Where are Magento2 modules located?
We need to know where our module should be located and where modules are located in the system in general.
- Usually, custom module is located in : <projectRoot>/app/code/<Vendor>/<ModuleName>
Then, we are creating our first module. First module location is: <projectRoot>/app/code/Bdn/Intro
Above I described a path where custom modules are located. Each eCommerce store that is being developed, often has a directory with custom modules:
<Vendor> is name of a company
<ModuleName> is a name of functionality that a developer extends from the core functionality or their own custom solutions.
For example, we are extending some checkout or wishlist functionality, then the name of module will be app/code/<Vendor>/Checkout or app/code/<Vendor>/Wishlist witch describes the area that has been changed.
- Modules that are installing via composer are located in:
/vendor/<Vendor>/module-<module-name>
For example magento module that is responsible for catalog rules:/vendor/magento/module-catalog-rule
In the nearest future, I’m going to describe how to deploy your module to the Packagist and install it via the Composer to any Magento2 project.
3) Creating the first module and describing its structure
Now we are coming to the most interesting part of this article. We will create a minimum needed for finding the module in the system.
So, let’s go step by step:
- Create a folder from the root of your Magento2 folder: <projectRoot>/app/code/Bdn/Intro
- Make a file registration.php in it with the content:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Bdn_Intro',
__DIR__
);
- Next create a folder etc inside our module <projectRoot>/app/code/Bdn/Intro/etc and make a file module.xml with the content:
<?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="Bdn_Intro" setup_version="0.0.1">
</module>
</config>
These are all the files that are needed for creating the first module.
Your module should look like screenshot below
Let’s describe these 2 files and 1 folder:
Files:
– <projectRoot>/app/code/Bdn/Intro/registration.php – it’s typically the same file in ALL modules. Registrate the module path in the system registry, for the future access to the files of the module.
– <projectRoot>/app/code/Bdn/Intro/etc/module.xml – the file contains basic information about your modules: name, version, dependencies.
Name – it’s just a name of your module, in format <Vendor>_<ModuleName>
Version – is a version of the module. Magento2 uses it when we make some updates in the database.
Dependencies – are module dependencies to the other modules in the system. They help to understand what modules require for correct work of the current module. (in this module we don’t use dependencies).
Folder
– <projectRoot>/app/code/Bdn/Intro/etc – this folder contains all module configurations like (config, di, events, webapi, routes, system, etc.), all files that are stored in etc is .xml files. That’s all what we need to know about it for now. Soon we are going to learn each configuration and understand how and why Magento2 uses it.
Also Magento recommends to create composer.json file in the root of your module <projectRoot>/app/code/Bdn/Intro/composer.json, but it’s not necessary for us, we will explore it in the article “How to add Magento2 module to Packagist”.
4) How to check that the module is working?
As we already know, Magento2 is a modulary system, we can show all the modules that are enabled and disabled in our system. Let’s check our module that was found in the system.
Next commands we are launching in the console (terminal).
Go to the root of your project
cd <projectRoot>
Show all the modules in the system
php bin/magento module:status
Enable module
php bin/magento module:enable Bdn_Intro
php bin/magento setup:upgrade
Run again the module status command, and find Bdn_Intro module in the list of enabled modules
php bin/magento module:status
Also you can check easily that your module was enabled in the file: <projectRoot>/app/etc/config.php
Open the file and find the line like this. That means that your module was enabled in the system.
'Bdn_Intro' => 1
For disabling the module, write this command
php bin/magento module:disable Bdn_Intro
And check it again
php bin/magento module:status
Then, check the file <projectRoot>/app/etc/config.php and find this. That means that we disabled the module successfully.
'Bdn_Intro' => 0
Conclusion
You have created your first basic module in Magento2. Learnt how to check the module status via command line. You understood what files are required for the module sctructure. Now you are ready to create bigger and more interesting modules.
Sincerely yours,
Bohdan.