Create a Module in Magento 2

Create a Module in Magento 2

Magento’s whole Core system is based on modules which is used for the different purpose for eg. Magento_Customer is used for Customer Functionality and Magento_Sales is used for Sales related Functionality. So In Today’s blog I will show you how we can Create a Module in Magento 2

Follow below steps to create a module in Magento 2.

  1. Create the module Folder Rk/SimpleModule.
  2. Create the registraion.php file.
  3. Create the etc/module.xml file.
  4. Run the bin/magento setup:upgrade script to install the new module. 

1. Create the module Folder

In Magento there are two location where you can find the third party module.

  1. app/code
  2. vendor
    • vendor

All the core module and those modules which installed using composer you will find in vendor folder.

    • app/code

All modules which are not install using composer you will find in app/code folder. 

So, For our module we will create folder under app/code

Path: app/code/Rk/SimpleModule

In above Path Rk is a Vendor name and SampleModule is our Extension name. Combination of Vendor and Module name makes the extension unique so it will not conflict with other modules.

2. Create the registraion.php file

Each module must have registraion.php file, which tells Magento how to locate the module.

create registraion.php file and paste below code.

Path: app/code/Rk/SimpleModule/registraion.php

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

3. Create the etc/module.xml file

module.xml file contains the following information:

  • Module name
  • Module version
  • Dependencies

Last Create module.xml file to complete our Simple Module and paste below code.

Path: app/code/Rk/SimpleModule/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="Rk_SimpleModule" setup_version="1.0.0"/>
</config>

4. Run the bin/magento setup:upgrade

Open terminal and Run the bin/magento setup:upgrade script to install the module.

You can check all other Magento 2 Useful CLI Command List here. 

after above command you can check your module entery in app/etc/config.php as 'Rk_SimpleModule'=>1 Here 1 means your module is enable.

 

Hope you find this Tutorial is helpful. Do comment if you face any issue or Contact me If you want any help or customization in your existing Project or Extension.
  1. Where I can find module Folder in Magento 2?

    In Magento there are two location where you can find the third party module.
    1. app/code
    2. vendor
     

  2. What is the use of registration.php?

    Each module must have registraion.php file, which tells Magento how to locate the module.
     

  3. What is the use of module.xml file?

    module.xml file used to register the module in the system. It contains the following information:
    – Module name
    – Module version
    – Dependencies
     

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top