Magento 2 : How To Create A Module
In this article, we will cover the process of creating a module in Magento 2 to make it simple for you. It’s important to note that the use of local/community/core folders, which were present in Magento 1, is no longer applicable in Magento 2.
To create a module in Magento 2
Follow the following steps to create a module:
- Step 1: Create a Module folder
- Step 2: Create module.xml file
- Step 3: Create registration.php file
- Step 4: Enable the module
Note: we are creating a module with the name of DemoModule and a vendor with the name of GDBlogger.
Step 1: Create a Module Folder
In the app/code directory create a folder with the vendor name. And create a folder with the module name inside the vendor folder.
Like this:
app/code/GDBlogger/DemoModule
Step 2: Create a module.xml file
Now Create the etc folder and add the module.xml file
Directory
app/code/GDBlogger/DemoModule/etc/module.xml
Code of 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="GDBlogger_DemoModule" setup_version="1.0.0"> </module> </config>
Step3: Create registration.php file
To register your module, you need to create registration.php file inside the module directory.
Directory
app/code/GDBlogger/DemoModule/registration.php
Code of registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, GDBlogger_DemoModule, __DIR__ );
Step4: Enable the module
Open the command terminal in Magento 2 root folder and run the following commands:
php bin/magento setup:upgrade Php bin/magento setup:di:compile Php bin/magento setup:static-content:dpeloy php bin/magenot cache:clean
After running these commands open the config.php file to confirm that your module is enabled.
Directory
app/etc/config.php
In this file, you will see the list of all modules, your created module should be available here.
Like this
‘GDBlogger_DemoModule’ => 1
Now your module is created successfully, you can add your functionality in the module as you want.
I hope this guide will be helpful to you. Please do not hesitate to contact us through the comments section if you have any further questions.
Also know, how to create a new admin user through command line.