Magento-2 How to create your first module

Software Engineer and Magento 2 developer building scalable eCommerce solutions, custom features, and optimized systems that improve performance and business.
Hi guys, today we are going to learn, how to create a module in Magento. But before we dive into how to create the module, we will first learn what is module and why we need to create a module in Magento.
Let's begin with the first question:-
What is module:-
A module is a group of directories that holds the logic for a particular task that is related to a specific business feature, actually, the whole Magento system is built upon modules.
Why we need to create a module:-
To customize the current given features of Magento or to add on some new features we create a module to hold our logic. Magento is an open-source framework and it strictly recommends us to not to do any customization work in its core modules, that means if we want to make any changes to its current functionality we will need to create our own custom module.
Now let's get back to our main topic, which is how to create a module.
Module creation is pretty easy yet so confusing if you are new to Magento. But no worries we are going to learn it step by step in a very simple way.
Steps to create a module:-
- Create a module folder.
- Create the module.xml file.
- Create the registration.php file.
- Run the bin/magento setup:upgrade command from the terminal.
Let's go through each of these steps in detail.
Step 1: Create a module folder
To create a module folder we first create the code folder inside the app folder i.e; app/code.
In Magento name of the module is defined as something like this VendorName_ModuleName and as we can see it contains two parts, the first part is the Vendor name and the second part is the module name.
Where vendor name should be the name of the company or organization under which this module is being developed
and module name should represent the name of the module according to its functionality.
For example:- Google_GoogleDrive
Now we will first create the Vendor folder inside the code folder.
app/code/Google
then we will create the module folder inside the vendor folder i.e;
app/code/Google/GoogleDrive
Note:- It is recommended that the first letter of the vendor name and module name should be a capital letter.
Step 2: Create the module.xml file
To create the module.xml file we will first create the etc folder inside our module folder.
app/code/Google/GoogleDrive/etc
Now create the module.xml file inside the etc folder.
app/code/Google/GoogleDrive/etc/module.xml
<?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="VendorName_ModuleName" setup_version="1.0.0">
</module>
</config>
This file is required for the module to exist.
Step 3: Create the registration.php file
Let's create this file in the same folder as module.xml i.e;
app/code/Google/GoogleDrive/etc/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Google_GoogleDrive',
__DIR__
);
This file tells Magento how to locate this module.
Step 4: Run the bin/magento setup:upgrade command from the terminal
Now that all the files and folders are created, we will run
php bin/magento setup:upgrade.
Running this command makes your module active and tells the Magento about the presence of the module.
Let's quickly summarize what we have learned so far.
To create a module we will need to create a vendor name folder inside the code folder and a module name folder inside the vendor folder.
Then we will create an etc folder inside the module name folder,
and at last, we will create two files module.xml and registration.php inside the etc folder.
Then run the command php bin/magento setup:upgrade. to make the module enable/active.
That's it for now. Take care and stay healthy.

