How to create new module in Magento 2

Create new module Magento 2, you need to follow step by step in this guide:

  • Step 1: Create folder of new Module
  • Step 2: Create file etc/module.xml
  • Step 3: Create file registration.php
  • Step 4: Run upgrade and check again 

Step 1: Create folder of new Module
The name of a module in magento is defined by VendorName_ModuleName. Go to path :
{path_root_folder_magento}/app/code. This is a area which storage the modules of site.
Create folder VendorName/ModuleName
Example: Atsharing/DemoModule
Step 2: Create file etc/module.xml
After step 1, go to the VendorName/ModuleName and create file etc/module.xml with demo content:

 

<?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="Atsharing_DemoModule" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>


Note:

 - name: name of module. Follow this structure VendorName_ModuleName.
 - setup_version: version module.
 - sequence: in sequence tag list modules. This param, i will discuss it in the later article.
Step 3: Create file registration.php
In this step, create file registration.php with demo content:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Atsharing_DemoModule',
__DIR__
);

Step 4: Run upgrade and check again 

Run setup upgrade cmd:
Go to the root magento path, open terminal and run commandline:

php bin/magento setup:upgrade

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

How to check in the module is created ?

 - Check file {path_root_folder_magento}/app/etc/config.php and find your module
 - Connect database and view table module_setup. Find name of module.

Download Module Demo here!

Share:

Copyright © 2020-present atsharing.com, Inc. All rights reserved.