Magento2 Module Development
Satish Kumar

What is a Magento Module or Extension?

The Module is a structural element of Magento 2 and it’s a whole system is built upon modules. A Magento 2 module can define external dependencies by using composer, PHP’s dependency manager.

As we know, the module is a directory that contains blocks, controllers, models, observer, helper, plugin, view, setup, etc. The etc folder contains admin ACL, admin menu, and configuration. Modules will be available in the app/code directory of a Magento installation, with this format: app/code/<Vendor>/<ModuleName>

To create a module, we need to discuss the following high-level steps :
  1. Create the module folder in the app/code folder.
  2. Create the etc/module.xml file in etc folder.
  3. Create the registration.php file in module folder.
  4. Enable the module.
  5. Run the upgrade command to install the new module.
Let’s we start go through each of the above steps in details :
1. Create the module folder

The name of the module is defined as VendorName_ModuleName. First part is vendor name and last part is module name. For example: Symphisys_CustomerPrice. Here vendor name is Symphisys and module name is CustomerPrice. Please check the following guide to create the folders:

app/code/Symphisys/CustomerPrice

2. Create the etc/module.xml file

This is very important to create etc folder and create one module.xml file in etc folder.It is non-core modules to override the functionality of core modules.

app/code/Symphisys/CustomerPrice/etc/module.xml

The inner contents in module.xml would be :

   <?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="Symphisys_CustomerPrice" setup_version="1.0.0">
       </module>
   </config>
3. Create the registration.php file

Each module must have registration.php file, which works, how to locate the module. Create the file app/code/Symphisys/CustomerPrice/registration.php. Then put the following content into it:

      <?php
          \Magento\Framework\Component\ComponentRegistrar::register(
          \Magento\Framework\Component\ComponentRegistrar::MODULE,
            'Symphisys_CustomerPrice',
                __DIR__
            );
4. Enable the modules

Complete above steps, we have already created the CustomePrice module and we need enable module with the following commands:

     php bin/magento module:status
     php bin/magento module:enable Symphisys_CustomerPrice              
5. Run the upgrade command to install the new modules
    php bin/magento setup:upgrade
    php bin/magento setup:static-content:deploy                  
We need to discuss each folder which one part of the architecture, as follows:

Api: API means for Application Programming Interface to allow the access of the data from an application. API also called as a middleman between a programmer and an application. It is the service contracts to the other environment like service interfaces and data interfaces.

Block: The View Models of our MVVM architecture.

Controller: Controllers is responsible for the handling the user’s flow while interacting with the system.

etc: It's configuration XML files, that the module defines itself and its elements routes, models, blocks, observers, and cron jobs inside a etc folder. The etc folder files may utilized by non-core modules to override the practicality of core modules.

Helper: The Helper is the class which is used for adding common functionalities to several features and can be used anywhere in the entire website. The Helper classes that hold the code used in more than one application layer.

i18n: It is holds internationalization CSV files and used for translation.

Model: Models is one of the inherent part of the MVC (Model-View-Controller) architecture. It is used in data operations, namely Create, Read, Update and Delete, on a database operation.

Observer: It is the particular classes that control the general behavior, performance, or change in the business logic of the store. They are executed whenever a specific event for which they were set to listen is triggered. It is holds Observers, or Models which are “observing” system events.

Setup: It is included install schema and data schema for the updating module and database. The migration classes, responsible for schema and data creation.

Ui: UI is the elements such as grids and forms used in the admin application.

view: It is responsible for design layout and template of modules. Layout in xml files and template in phtml files for the front-end and admin application.

Share
Tags
Categories

Satish Kumar
Project Lead
Symphisys
May 15, 2020