Hey, Amasty blog readers!
How is your preparation for the Magento 2 certification exam?
This time we asked Alexey Motorny, who passed Magento Certification in 2014 and who has been developing modules for 6+ years already, to describe the Magento developer directory structure and how to find the files responsible for certain functionality.
Magento 2 directory structure [brief overview]
The best way to look into the Magento directory structure is to use a practical example. We’ll consider the main files locations and how to find the files responsible for needed functionality. Let’s take the case of the most extensive Magento modules -- Module-catalog.
The following is the module components:
Let’s go over it.
API
The directory contains interfaces for models and classes for our module. For instance, interface Magento\Catalog\Api\TierPriceStorageInterface allows us to see the structure, parameters, methods, and types of values passed to them for the module’s work with tier prices Magento\Catalog\Model\Product\Price\TierPriceStorage.
Block
The directory contains PHP files for information processing. The processed information will be later displayed to users by using the template files. Almost any content we see on Magento pages is displayed with the use of blocks. Most importantly, the directory contains the Adminhtml folder. We put blocks used for the backend part of Magento into the folder.
Console
The directory is used for storing the PHP files that are responsible for console programs execution. In our case, Console/Command/ImagesResizeCommand.php processes commands for product images resizing.
Controller
The directory processes requests sent to the server. Controllers process the input parameters and decide what information should be returned upon the request. For example, vendor / magento / module-catalog / Controller / Product / View.php is responsible for content displaying on a product page.
Cron
The directory contains files that will be run on a cron launch.
CustomerData
The directory contains PHP files and responsible for processing information for sections. Magento 2 has a special functionality, which allows for processing, updating and transferring the information asynchronously. For example, to update the information on purchases and top cart.
Etc
This is, probably, one of the most important directories. It contains the main configuration files of the module. You can find the module description, resources settings, fields description for Store Configuration, API description, plugins, observers and others.
To find more information, follow the link. Configuration let us know what module makes and what settings are used for this.
Helper
The directory contains PHP files helpers that include aggregated functionality that can be utilized in different module parts.
i18n
The directory contains the CSV files. The files allow you to transfer the module to different locales.
Model
The directory contains all the files that work with database tables. They allow for receiving and processing information from the tables, adding and deleting it. The simplest example of it is getting product information.
Observer
The directory contains PHP files responsible for executing commands from the listener. Magento code includes events, which we can apply to add listeners with the use of vendor/magento/module-catalog/etc/events.xml. We can also use the events to change or read the needed information.
Plugin
The directory contains plugin files. The plugins allow us to modify certain functions if necessary. They are described in the configuration file vendor / magento / module-catalog / etc / di.xml. Learn more about the directory from here.
Setup
The directory contains scripts interacting with the database. For example, we will use the directory if we need to create a table when installing the module.
Test
The directory is used for integration tests. All the required test files are located here.
UI
In Magento 2 Admin Panel many functional elements are developed using the technology of UI components. This directory contains files with data generation, which are later receive, process and show the components on a display.
View
In this directory you can find all the visual files: js css html phtml xml, or, simply, all the files we see on the frontend. The files are divided into three categories relying on the area of action:
Frontend Files
The following files can be found in each directory.
Layout (the .xml files)
The file defines what blocks and in what order will be shown on the display.
Templates (the .phtml files)
The files contain HTML structure a user will see on the display.
UI_component (the .xml files)
The files that contain the configuration of the UI components decide where components with what templates will be displayed.
Web
We use the directory to locate .js and .css files, as well as images required for markup.
We also need to mention the view/frontend/web/template directory. It’s used to locate .html files applied for UI components.
Registration.php
The file includes the directory in which a component is installed by vendors (in production environment). By default, components are automatically installed in the /vendor directory. See component registration for more information.
Composer.json
The directory defines component dependencies, as well as other metadata. To learn more about the directory, follow the link.
We hope it was helpful! Drop us a line if you still have questions on the topic.
P.S. Don’t forget to stay tuned and check our next posts in preparation for Magento 2 certification exam!