Magento 2 is known as a robust e-commerce platform that supports custom modules. These modules allow users to personalize and expand their online stores according to specific needs. This article highlights the core aspects of building a custom module in Magento 2, with specific focus on structure and terminology.

What is a Custom Module in Magento 2?

A custom module within Magento 2 is a package of code that provides additional functionality to your e-commerce website. Depending on the need, this can range from minor tweaks to extensive additions. Modules provide seamless expansion of Magento 2 core functionality without direct changes to core code, facilitating upgrades and maintenance.

Structure of a Magento 2 Custom Module

Each Magento 2 custom module has a standardized and well-considered structure. Below is a list of the main folders and their functions:

  • etc: This folder contains configuration files that define routes, models, blocks, observers and cron tasks.
  • Block: Here are the ViewModels for the front-end, essential for data presentation.
  • Controller: This folder houses controllers that control the interaction between the user and the module.
  • Model: Here you will find Models and ResourceModels, which are the core of the module's business logic.
  • Observer: Observers "observe" system events and respond to them.
  • Setup: Responsible for database schedules and changes to them.
  • Ui: For UI elements such as grids and forms in the management interface.
  • view: Contains layout and template files for both front-end and management interface.
  • i18n: For international support through translations.
  • Helper: Utilities and classes used in multiple layers of the application.
  • Test: Here you can write tests for your module to verify operation.

Composer and External Dependencies

Magento 2 integrates with Composer, PHP's dependency manager, for managing external dependencies. This facilitates the integration of external libraries such as Zend Framework and Symfony.

Conclusion

Developing a custom module in Magento 2 requires an understanding of the underlying structure and terminology. Thanks to the standardized layout, modules are easy to manage and expand. By leveraging Composer, external resources can be seamlessly integrated. This allows you to unlock the full potential of Magento 2 and optimize your online business.

Additional note: Magento 2's Community Edition (CE) and Enterprise Edition (EE) both build on the same core of Magento 2, meaning they share many common functionalities. Therefore, modules developed specifically for the CE version are usually also usable within the EE version without problems.

Directory Tree of a Typical Magento 2 Module

Magento_ExampleModule/
│
├── etc/
│   ├── module.xml
│   ├── adminhtml/
│   │   └── system.xml
│   └── frontend/
│       └── routes.xml
│
├── Block/
│   ├── Adminhtml/
│   │   └── BlockFile.php
│   └── Frontend/
│       └── BlockFile.php
│
├── Controller/
│   ├── Adminhtml/
│   │   └── ControllerFile.php
│   └── Frontend/
│       └── ControllerFile.php
│
├── Model/
│   ├── ModelFile.php
│   └── ResourceModel/
│       └── ResourceFile.php
│
├── Observer/
│   └── ObserverFile.php
│
├── Setup/
│   └── InstallSchema.php
│
├── Ui/
│   ├── Component/
│   │   └── Listing/
│   │       └── Column/
│   │           └── ColumnFile.php
│   └── DataProvider/
│       └── DataFile.php
│
├── view/
│   ├── adminhtml/
│   │   └── ui_component/
│   │       └── examplemodule_form.xml
│   └── frontend/
│       ├── layout/
│       │   └── examplemodule_index_index.xml
│       └── templates/
│           └── templatefile.phtml
│
├── i18n/
│   └── en_US.csv
│
├── Helper/
│   └── Data.php
│
└── Test/
    └── Unit/
        └── Model/
            └── ModelTest.php