How to Create a New Module in Magento
Why Create New Module in Magento
Before diving deep into how to create a new module in Magento, let's first get the motivation behind doing this or the philosophy behind this.
Module is one of the most important core components and units of customization in Magento. The whole system of Magento is built on top of the modular concept. Modules provide business features, with supporting logic and a particular module is supposed to cater to a specific product or business feature which can be extended or customized. Or even we can add new product features or business functionality with modules. And for all of these, we can do it by creating a module.
So, I think now we're convinced that if we would like to customize or personalize Magento for creating a personalized store or application, we have to create a new module for that.
Steps to Create a New Module in Magento
There are 5 high-level steps we need to follow to create a module which are:
- Creating the module folder where all of our customized code will retain.
- Creating the etc/module.xml file.
- Creating the registration.php file.
- Running the bin/magento setup:upgrade script to install the new module.
- Checking that the module is working.
Creating the module folder
Before creating a new module folder, first of all, we need to know where to create the new module folder. And to answer this question, we need to answer first what is our purpose for creating a new module as the folder location depends on that.
CASE#1: If we're interested in building a module for a specific project:
If we're building a module for a specific project, it is best to choose the app/code/Magento/ folder and commit to the project’s repository.
CASE#2: If we're interested in building an extension that can be reused:
If we're building an extension that can be reused, it is better to use composer to create it and put this module in the vendor/magento/magento-* folder.
Note
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 we need to define the vendor and module names. Let's take an example, let’s name the vendor “EcommerceCompany” and the module “ExportInfo”.
Let’s create the folder app/code/EcommerceCompany and inside this folder place another folder: ExportInfo. We can create these folders using the command line and in that case, the code would be:
cd to the root folder
mkdir app/code/EcommerceCompany
mkdir app/code/EcommerceCompany/ExportInfo
Creating the etc/module.xml file
The next step is to create an etc/module.xml file that is required for the module to exist maybe by following the below steps.
- Create a folder named etc
- Create a file named module.xml inside the etc folder.
Magento expects the following information to be present in this module.xml file which are specific to this module.
- Module name - which is defined by the folder we just created for this module and the structure should be VendorName_ModuleName and for our case it will be EcommerceCompany_ExportInfo
- Module version - is used for upgrading versions which basically contains the current version of database schema and data.
- Dependencies - is used to declare the dependency of this newly created module with other modules.
<?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="EcommerceCompany_ExportInfo" setup_version="0.0.1"> <sequence>
<module name="Magento_Catalog"/> </sequence>
</module>
</config>
Creating the registration.php file
The next step for successful module creation in Magento is to create a registration.php file that tells Magento about the location of the module and is a must for every module. It's a standard file like the below example for any module you create and only the module name you need to change.
Sample code for registration.php file:
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',
__DIR__
);
Running the setup:upgrade Script to Install the Module
Yeah, we've already created the basic building blocks for a new module. Now it's time to install the module and for the same, we can run the below command to tell Magento that boss we have a new module, please take care. 😀
php bin/magento setup:upgrade
Let's Check whether My New Module Is Working or Not
Yeah, hopefully, our new module is running though we've not put any functionality or new code to implement some personalization. But the skeleton is ready and the module is running. To verify this statement, you can check the file app/etc/config.php whether our new module name is present there or not.
And that's all for creating a new module. You are now free to put your customized code into your newly created module to develop some new feature or business functionality.
Comments
Post a Comment