How to create your first custom module in Magento 2?

How to Create a Custom Module in Magento 2?

In this tutorial, we are going to to create a custom module. Follow these steps to create a module.

The module is basically a structural element of Magento 2. A whole new system is built upon modules in Magento 2. The first step towards building a customized eCommerce store is building a module.

To build a module in Magento 2, you need to understand the architecture of the module directory. Magento 2 architecture has two locations to create the module: app/code folder and the vendor folder.

In Magento 2,

1. All the core modules located at vendor/magento/magento-*

2. If you have cloned magento project from github, You can find core modules located at app/code/Magento

3. if you are building an extension to be reused, it is recommended to use composer to create it and put your module in vendor/<YOUR_VENDOR>/module-something folder.

In Magento 2, each module name consists of two partsfirst is the vendor and second is the module itself. Basically, modules are grouped into vendors, so you are required to define the vendor and module names. For an example, lets name the vendor ‘Jigar’ and the module ‘CustomModule’.

Alright, now let’s do something more practical. Here’s the step-by-step tutorial to create the Magento 2 module.

Step 1 : Create the Magento 2 Module Directory

Let’s create the folder in the directory app/code to build the extension in a given structure defined as VendorName_ModuleName. I will create Jigar_CustomModule, where the first part is the name of the vendor provider and the second part is the module’s name.

app/code/Jigar/CustomModule

We can do this manually or else we can create those folders via using below command line as below.

Step 2 : Declare our custom module

The file is necessary for the module to exist. The file contains information such as Module Name, Module Version and Dependencies.

The module name is defined by the folders we just created. In Magento 2, class names must follow the folder structure.

Because we created the folders Jigar/CustomModule, the module name will be Jigar_CustomModule and all the classes that belong to this module begin with Jigar\CustomModule.

For an instance,

Jigar\CustomModule\Observer\Test.

to declare our magento 2 module, create etc/module.xml file inside <MAGENTO_DIR>/app/code/Jigar/MyModule/ Directory.

app/code/Jigar/MyModule/etc/module.xml

To create etc folder & module.xml file using command-line, run below commands in your magento 2 root directory.

Add following content inside module.xml file to declare your module.

Step 3: Register the Created Module in Magento 2

The module has to be registered in the Magento 2 system using the Magento Component Registrar class that defines how to locate the module. Continuing our module, create the file registration.php in the module root directory:

to create using command-line run below commands on magento 2 root directory

app/code/Jigar/CustomModule/registration.php

This file calls the register method of ComponentRegistrar class with two parameters, the string Module tells the type we are going to register, and the other is our module name Jigar_CustomModule. This step holds important information in Magento 2 module creation.

Step 4: Run the Command

The steps above should result in the creation of a simple module. To notify Magento of its presence, run the commands below in the magento 2 root directory :

Step 5 : How to check if the new module is active or not?

You can check enabled magento 2 modules by running below magento 2 command.

If your module name exists in the list of enabled modules list, that’s all your module is now active.

If your module is showing in list of disabled modules, you can simply enable it using below command.

Currently, we haven’t added any useful code to our module – so it is still empty. So, to verify that it has been recognized, check the file app/etc/config.php. 

It consists of a list of auto-generated modules that are active.

You can search your module name by using command line by running below grep command.

You will see the output with your module name and status 1 which indicates it is active.

That’s all, now you learned how to create custom module in magento 2.

However, if you still need any queries let me know in the comment section or feel free to reach out to me anytime.

Leave a Reply

Your email address will not be published. Required fields are marked *