Magento modules are very useful to add additional functionality or modify the existing without touching the core code. Magento modules can be located in /app/code and /vendor directories. Usually, the modules that are installed via composer can be found in the /vendor directory. While others can be found in the /app/code directory.
Inside the module directory, the following directories and files are can be found. However, all the following directories are not required but the basic knowledge of these directories can be helpful while working with Magento modules :
1. Api
This directory contains PHP interfaces for business logic that are covered by the respective module.
Data – Contains interface for data transfer objects that are used with Api interfaces. These (Api+Data) are also called module Service Contract.
2. Block
Contains view layer logic classes. The method of these classes are available to respective PHTML template.
3. Console
It contains command-line interface commands which are executed using bin/magento
. When we have to create the custom console command to execute with bin/magento
then we need to add the command classes inside this directory.
4. Controller
contains request processing logic like it contains all actions for the module. Controller\<Controller>\<Action>
is the PHP classes template for all module actions.
For instance, for <front_name>/<controller>/<action>
URL query the Controller/<Controller><Action>.php
file.
5. Cron
Contains tasks classes (cron tasks) that are executed via CRON. Generally, the etc/crontab.xml file contains the reference for these task classes.
6. CustomerData
Contains classes that make server-side session data available to the storefront javascript.
7. etc
Contains module configuration like module.xml (required file) etc.
It also contains the area-specific directories like frontend, adminhtml etc to make area-specific configuration.
8. Helper
Contains the helper classes which contain some common methods. Other classes can use these methods by injecting the respective helper class in the constructor parameter.
9. i18n
Contains source CSV files translation.
10. Model
Contains ORM entities and implementation of Api interfaces. It generally contains Model, Resource Model and Collection classes.
11. Observers
Contains PHP files of observer classes. These classes are executed by the event listner listener which are defined in the etc/events.xml (or inside area-specific directory in /etc) file.
12. Setup
It contains PHP classes directory related to migration for managing database tables and content.
13. Ui
Contains auxiliary PHP classes for UI components, like modifies, data providers, etc.
14. view
It contains templates, CSS, JS, images, layouts, UI components files. The internal structure of this directory consists of <area> and base, with base storing the files that relate to all areas. <area> and base have similar file structures. adminhtml and frontend area can be found in this directory. Under these areas you can found the following subdirectories:
- email – contains email templates.
- layout – contains layouts XML files. Commonly, the layout name is formed according to this template: {route_id}_{controller}_{action}.xml
- templates – contains phtml templates’ files
- page_layout – contains files, describing Magento page types (1column.xml, 2columns-left.xml, etc,)
- ui_component – contains .xml files with UI components description
- web – contains static files, template files for KnockoutJS, JS, theme files and images
15. ViewModel
Contains view layer logic classes for the template files. These are PHP model classes as part of a model-view-viewmodel (MVVM) implementation. It allows developers to offload features and business logic from block classes into separate classes that are easier to maintain, test, and reuse.
16. registration.php
The required file that registers the module in Magento.
17. composer.json
Contains composer package configuration
Rule of thumb for directories naming convention inside the module:
– if the directory contains PHP classes it’s directory name starts with capital letter eg. Block, Controller. This is because of PHP PSR-4 autoloading standards that map PHP namespaces and classes to directory and files.- if the directory contains other things instead of PHP classes eg etc, i18n or view then the directory name should be lowercase.