Magento 2 Folder Structure – File & Module Directory Structure
Just like the operating system in our computer requires the source files, the resources stored on the Magento 2 server power the complete functioning of the online store. Every element of the design, set of functions, modules, database, and other files are stored on the Magento server, making the flawless operations possible for the merchants and the customers.
Have you ever wondered where the default modules reside in the Magento directory? or How the files and resources are structured in the Magento directory? Read this blog post to find out answers to such questions.
Magento 2 stores the source files in a very structured and sophisticated manner. It is essential for the users to understand the directory structure in Magento 2 before diving deep into the versatility of this eCommerce platform. It is also essential for the budding developers to clear the Magento 2 Folder Structure concept, as it will help them better understand how the backend works in Magento 2.
In this article, I have explained the directory structure of Magento 2 in simple words, yet in detail!
Let’s get started!
Magento 2 Folder Structure
The Magento 2 root directory contains the following folders:
Each of the folders in the Magento 2 root directory is explained below in brief with subfolders:
The app folder in Magento 2 contains the main Magento code along with the design files. The subfolders of /app folder are explained below in brief.
- app/design subfolder contains all the design resources for both the admin panel and frontend.
- app/design/adminhtml subfolder contains all the LESS & CSS files for the backend admin panel.
- app/design/frontend subfolder contains the design resources responsible for the storefront look.
- app/etc subfolder contains the global di.xml file that includes dependencies and classes required by the object manager.
- app/i18n contains the different language packs installed on your Magento 2 store.
- app/code may also be created while developing new modules in Magento 2.
The bin folder in Magento includes the Magento file, which contains the Command-line Interface (CLI) script. It is responsible for the basic functionalities, including controlling cache, managing modules, indexing, etc.
The generated folder in the Magento 2 folder directory contains all the codes generated for creating new classes as required.
The lib folder in the root directory of Magento 2 contains the non-module-based codes and libraries. It is a pool of codes and libraries apart from the Magento core.
- lib/internal subfolder contains the server-side libraries and fonts.
- lib/web subfolder contains the client-side libraries solely for core Magento resources.
The phpserver folder contains the router.php file used for implementing PHP in the Magento server. It is not advised to tweak the router.php file as it may result in a security loophole in the system.
As the name suggests, the pub folder contains all the files in Magento 2 that are publicly accessible. It contains all the resources and files required and generated by Magento to create the frontend. The index.php file is also present in this folder.
The setup folder in the root directory of Magento 2 contains all the resources and files required during the set-up of Magento 2 after installation.
The var folder contains all the logs, cache and error reports of the sessions and the generated classes. Some of the common subfolders generated under this folder are:
- var/log subfolder contains all the log files generated during the sessions, which includes system.log & exception.log.
- var/cache subfolder contains all the Magento cache files.
The vendor folder contains the composer.json. The files and folders included under the vendor folder is defined by the composer.json file.
Magento 2 Module Directory Structure
The app folder is where the default modules reside in the Magento 2 directory. The modules are stored in app/code/ directory and contain the following common files and folders:
Each of the Magento 2 module directory folders is explained below in brief:
The block folder in the module directory contains the PHP view classes as a part of the Model View Controller(MVC) vertical implementation.
The controller folder contains the classes as part of module logic.
The etc folder includes the configuration files such as module.xml.
The Model folder contains all the Database-related PHP model classes.
The Observer folder contains all the files required for executing the listener’s commands.
The view folder contains the static files such as design templates, email templates, layout files, etc.
The theme file directory also contains a similar folder structure in Magento 2 as the module directory. You may love to read our detailed blog post on creating a child theme in Magento 2.
I hope this blog post will help you get started with Magento 2 folder structure and directory. One thing to mention here is that the Magento 2 community and enterprise versions have slightly different directory structures. You can also refer to the official Magento 2 resources on components structure to understand how the core components interact in Magento 2.