Magento 2 Directory Structure

Magento 2 Directory Structure

Magento is based on Object-Oriented Programming (OOP) structure concept, made with MVC design pattern in PHP scripting language. Magento 2 Module File/Folder structure is too complicated. Magento 2 folder structure has been changed a lot from the previous version of Magento platform. We are going to cover all files and folders within Magento 2.

In this article, we will take a look at Magento’s directory structure. Each structure has its own list of directories. File Structure contains blocks, controllers, helper, models etc. All related to a particular business feature. In order to ensure the component type works properly, you need to follow the predefined file structures. All of the core Magento files are located in the vendor/magento/ directory with some supporting JavaScript and CSS files being stored in lib/.

Third-party files can be found in their respective vendor/vendor-name/module-name/ directories.

For your customisations:

For modules, use app/code.

For storefront themes, use app/design/frontend.

For Admin themes, use app/design/adminhtml.

For language packages, use app/i18n.

/Api: Service Contracts
The /Api directory stores the contracts or interfaces that are exposed to the API. An example of this would be Magento\Catalog\Api\CategoryListInterface.

/Api/Data: Data Service Contracts
This folder contains interfaces that represent data. Examples of this would be a product interface, a category interface, or a customer interface. The concrete implementations of these interfaces usually do little more than provide getters and setters for data.

See Magento\Catalog\Api\Data\ProductInterface for an example.

/bin : The directory contains the magento file or a CLI script. The Magento CLI executable script is stored in this directory. Using this script, Magento commands will be activated, which can become helpful in clearing cache or reindexing.

/Block: View Models
Blocks can be considered as template assistants. Very little functionality or business logic should be done in templates – that is the responsibility of the Block.

/Console: Console Commands
When running bin/magento on the command line, a list of available commands to run is output. The code for commands should be placed inside the /Console directory.

/Controller: Web Request Handlers
This directory stores controller classes for web requests.

/Cron: Cron Job Classes
Definitions for cron jobs are stored here.

/dev : Magento 2 test scripts and a few tools for development are placed in the folder.

/etc: Configuration Files
Any files directly within this directory are applied globally. You can restrict the area these files belong to by placing it in the relevant sub-directory – etc/frontend/, etc/adminhtml/ etc.

NB: Some files MUST be placed within a particular area whilst others MUST be global.

/generated : This is the place where Magento’s generated code is stored. In the default setting, if the class is injected in a constructor, the code will be generated by Magento to create non-existent factory classes.

/Helper: Aggregated Functionality
Small, reusable snippets of code should be stored in a helper class.

/i18n: Localisation Files
This directory contains the translation CSV files for the module. These CSV files contain two columns, from and to. In case you want to create a multi-language store which can suit your customer needs better, you can use this folder.

/Model: Data Handling & Data Structures
A self-explanatory directory.

/Model/ResourceModel: Database Interactions
The classes stored in this directory dictate how data is stored and retrieved from the database. Any direct database interaction should be done within these files.

See vendor/magento/module-catalog/Model/ResourceModel/Product.php for an example.

/Observer: Event Listeners
When Magento fires an event, certain observers are called – decoupling the system. Magento Commerce integrates with RabbitMQ which allows even more control and reliability to this process. Event data should be able to be stored and then run in a queue at a later time.

Observers MUST implement the Magento\Framework\Event\ObserverInterface. The PHP class should follow the stand of using TitleCase while the event name should be snake_case.

Observers should not contain any business logic. This logic should instead be placed in another class and injected into your observer.

/pub : It should be set as your web root on the server you use (Apache, Nginx).
index.php controller is also contained in the folder.

This directory stores your module’s plugins.

/Setup: Database Modification. This contains all files which are related to Magento’s installation setup.

Stores the following files:

InstallSchema.php : Sets up table and column schema when the module is installed.

UpgradeSchema.php : Modifies table and column schema when the module version is upgraded.

Recurring.php : Runs after every install or upgrade.

InstallData.php : Sets up data when the module is installed. An example would be adding a custom CMS block.

UpgradeData.php : Modifies data after the module is installed and when the module version is upgraded.

RecurringData.php : Applies to data after every install or upgrade.

/Test: Module Tests
This directory stores your module’s tests. Tests can be run via the command line using bin/magento dev:tests:run.

/Ui: Data Generation Files

/var : caches and generated classes are all located in the folder.
the var folder includes cache, composer_home, generation, log, di, view_preprocessed, page_cache files.

/vendor : The folder is generated by composer using composer.json file.

/view/[area]/templates: Block Templates
The counterpart to a Block is a template to render the HTML for the block. Whilst the block (in /Block) represents the business logic, the template represents how the results of the business logic are shown to the user.

/view/[area]/web: Web Assets
Web assets such as images, CSS, JavaScript, LESS, and SCSS are stored in this directory.

view/[area]/web/template: JavaScript Templates
HTML templates that can be requested asynchronously via JavaScript are stored in this directory. These files often contain KnockoutJS declarative bindings.

/view/adminhtml/ui_component: Adminhtml UI Components
This directory contains the XML configuration for UI components. They are used to represent distinct UI elements, such as grids and forms, and are designed to provide flexible user interface rendering. Most Magento Admin grids (such as the Catalog Product grid and the Customer grid) are composed with UI components. The checkout on the frontend is also a UI component.

That is how Magento 2 directory structure looks like, hope it was helpful. Leave your questions and comments in the section below and I will get in touch as soon as possible.

🙂 Happy Coding. Keep Liking & Sharing 💻


So Home
Tagged , , , ,