How to Create New Module in Magento 2?

| |
Comments: 0
How to Create New Module in Magento 2?

A Magento module is a package collection of Php, xml and phtml files and these files are included in folders like blocks, controllers, helpers and models. Modules provide specific business feature that extends the functionality of the online store.

In this blog, we will learn how we can create a custom module in Magento 2 and for that, we will follow the below simple steps to create a module:

  • Create the module folder
  • Create the module.xml file
  • Create the registration.php file
  • Create the composer.json file
  • Enable the Module

Step 1:Create the module folder

For creating a module folder, first define a module in app/code directory with app/code//. For example, our Vendor name is MageAnts and Module name is Blog.

Step 2:Create the module.xml file

Now let’s create module.xml file for declaring a module.

Create module.xml file at app/code/Mageants/Blog/etc/module.xml and write below code.

<?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=”Mageants_Blog” setup_version=”1.0.0″> </module> </config>

Step 3:Create the registration.php file

To register the module, create registration.php file at app/code/Mageants/Blog and write below code:

<?php
\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, ‘Mageants_Blog’, __DIR__ );

registration.php file is a standardized file that follow the same pattern in all Magento modules.

Step 4:Create the composer.json file

Now create composer.json file in app/code/Mageants/Blog and add below code:

<?php
{
“name”: “mageants/blog”,
“description”: “”,
“require”: {
“php”: “~5.5.0|~5.6.0|~7.0.0”
},
“type”: “magento2-module”,
“version”: “1.0.0”,
“license”: [
“OSL-3.0”,
“AFL-3.0”
],
“autoload”: {
“files”: [
“registration.php”
],
“psr-4”: {
“Mageants\\Blog\\”: “”
}
}
}

Step 5:Enable the Module

After following above steps, check the module status using the below command:

php bin/magento module:status

Currently, module is disabled, so for enabling module please run below command:

php bin/magento module:enable Mageants_Blog

The another way to enable module, go to the app/etc/config.php file and add our module as follows:

‘Mageants_Blog’ => 1,

After follow above steps, run the below commands:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

php bin/magento cache:flush

Recommended Read: How to Create Custom API in Magento 2?

Conclusion:

Using above blog, you can easily understand How to create new module in Magento 2. If you have any query then contact us.

Leave a Reply

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