Magento 2 is one of the leading platforms for B2B, B2C, and agencies because it’s flexible and customizable. But for developers, the Magento directory structure is quite complex when it comes to customization or building application components.
Magento 2 Folder structure contains an app where the custom code development works and, most importantly, where the business logic executes.
In this article, I’ll tell you why folder structure is important, the hierarchy of Magento folder structure, and the key differences between Magento 1 and 2 folder structures.
Importance of Folder Structure
The folder structure in Magento is important for different reasons. Here are a few of them.
- It helps to organize the different files and resources of the Magento application, making it easier to find and maintain them.
- It allows the Magento application to function properly by providing a clear separation of responsibilities and a hierarchy of files and resources.
- It helps to ensure that the Magento application can be extended and customized easily by allowing developers to add new code and resources in the appropriate locations.
- It helps to improve the security and stability of the Magento application by keeping sensitive files and resources separate from publicly accessible ones.
Magento Directory Structure
The directory structure of Magento consists of the following 3 types.
- Basic Structure
- Module Structure
- Design Structure
Each structure has its own list of directories. First, let’s talk about the standard directories of Basic structure in Magento 2. Let’s see them in detail.
Magento Folder Structure
Magento 2 folder structure has greatly changed from the previous version of Magento. Let’s start by naming the “Root directory” as the installation directory of the Magento platform. If you navigate through the root directory of Magento 2, you’ll see the following folder structure:
Each Magento 2 folder contains a subdirectory of files with pre-defined business features. Let’s look deep into it and understand how it works.
- app – This folder contains the core files of Magento 2, including configuration, modules, and themes files.
By default, the Magento repository’s basic installation contains three main folders, i.e., design, etc., and i18n. The code folder is missing, but all the custom code development and third-party integration modules are in the app/code with the directory app/code/VendorName//ModuleName.
Store custom design theme structure is under the app/design/frontend subdirectory, and the admin theme is under the app/design/adminhtml subdirectory.
To store language packages, the app/i18n directory is your need to help you create a multi-language store.
Besides, to avoid custom modules being mixed with the core modules of Magento, you need to add all Magento core modules in the vendor directory. This is because, unlike in Magento 1 version, where all their Magento core modules had to be committed to their Git repository, with Magento 2, these two are separated.
- bin: It contains the Magento 2 CLI executable script feature. Using CLI would require SSH access to the server. The Magento 2 CLI can help install modules, upgrade the application core, reindex, compile, etc.
- dev: This contains various developer tools, such as Magento Test Framework (MTF) for functional test units.
- generated: This includes two subdirectories code and metadata with all the Magento 2 generated code of all the injected classes. The command can remove it: rm -r <magento_root>/generated/*/* to re-generate the nonfactory classes by the composer.
- lib: This directory contains libraries packed separately from the Magento 2 core libraries.
- phpserver: This folder contains the Router.php file, the functional unit of the php Built-in web server. However, working on this file for a security breach is strongly recommended.
- pub: Stores the file index.php and all generated static files and site media. It should be set as your web root on the server side. As the name says, it contains all the publicly accessible files.
- setup: Several important packages, such as the performance toolkit, database scripts, and installation setup, are available in this setup directory.
- var: The var folder contains raw sessions, cache, database backups, and cached error reports. This regenerates the contents of numerous subdirectories when running the command bin/magento setup:di:compile.
- var/log: This stores Magento log files are mostly exception.log, debug, and system.log files.
- var/cache: contains all of Magento’s cache. You are recommended to use the php bin/magento cache:clean command to clear your cache to see your development changes.
- vendor: The composer generates the vendor directory using the composer.json file. This folder includes various packages that have been defined under the composer.json file.
Module File Structure
A module file structure looks like this:
Working with custom code development, some directories are common for the logic in Magento 2 module. Let’s understand this in detail:
Here are some common module directories:
- Block: This contains all PHP view classes with no html code as part of module logic Model View Controller(MVC) vertical implementation.
- Controller: It Includes all PHP controller classes for receiving requests to the page as part of module logic MVC Framework vertical implementation.
- etc: includes configuration files, especially the module.xml, which is required for the basic module.
- Model: includes PHP model classes as part of module logic MVC vertical implementation.
- Setup: Includes classes for module database structure and data setup. These data are invoked when installing or upgrading the database.
Additional folders can be added for configuration and other ancillary functions for items like plugin-ins, localization, and layout files.
- API: contains any PHP classes exposed to the API. Developers use this directory to create interfaces and write configuration files and implementations.
- Console: contains CLI commands. Create skeleton classes for a specific command to run for the module.
- Cron: contains cron job definitions to run the script manually.
- CustomerData: contains section files.
- Helper: contains aggregated functionality.
- i18n: contains localization files.
- Observer: contains files for executing commands from the listener.
- Plugin: contains any needed plug-ins.
- UI: contains data generation files.
- view: contains view files, including static view files, design templates, email templates, and layout files.
All modules require the following files:
- registration.php: This is the file that will register your component with Magento. In this file, the component’s root directory name is used as the component name. Therefore, by default, components are installed in the
<Magento root dir>/vendor directory.
- etc/module.xml: This is the file that defines the basic information about the component, for example, component dependencies and version number. The version number will be used to determine which schema and data to update when you execute bin/magento setup:upgrade.
- composer.json: This is the file that defines the dependencies that the component will need at runtime.
All right, after this relatively brief introduction to the system architecture and module file structure, let’s do something more concrete, shall we? Next, we will go through the Magento theme folder structure so that you are comfortable with Magento 2 components.
Magento Theme Structure
It’s trivial to start with customization until to know the basics. Let’s understand the Magento 2 typical theme directories:
A typical magento theme contains the above directories. However, if you need to create a custom theme, you must create a module first.
- etc: Includes configuration files like the view.xml file with image configurations for all images and thumbnails.
- i18n: Translation dictionaries, if there any available.
- media: Theme preview images can be put in this directory.
- web: Optional directory with static files organized into the following files:
- css/source: Includes a theme’s fewer configuration files invoke mixins for global elements from the Magento UI library and the theme.less file, which overrides the default variables values.
- css/source/lib: Includes view files that override the UI library files stored in lib/web/css/source/lib.
- fonts: you can place the different fonts for your theme in this folder.
- images: folder of static images.
Language Package File Structure
This is the typical directory structure for the language en_us package as follows:
This directory contains registration.php to register the package and composer.json to download the dependencies and license if any. The top-level directory required for a language pack is the top-level directory. For better performance, I recommend using the directory name that matches the ISO code to identify the locale.
Magento 1 vs 2 Folder Structure
As you know, Magento 2 file structure is more optimal than Magento 1.x file structure. Let’s quickly review the notable changes between these two platforms.
The directory structure looks like this:
Major changes are applied at the root level. You will first identify that some new folders are introduced in Magento 2, and some are removed.
|Magento 1.x||Magento 2.x||Significant Change|
|Custom Code Name||Custom code is in the local pool folder, i.e., app/code/local/vendor/mymodule.||The custom code is based on the module is the subdirectory of app/code/vendor/mymodule.||No more local folders.|
|Module Folder Structure||Module is in three main directories of the app: code, community, and local.||The component module resides in a single folder inside the app: VendorName/ModuleName||No more Code, community, and local pools. (Lack of code pools)|
|Design Theme Structure||Theme is created with two directories, i.e.,
|Themes are created in app/design/frontend with the inheritance of subdirectories of vendor/theme-name, i.e., app/design/frontend/vendor/theme_name
||Skin folder is removed. All assets, such as .css and .js files, are moved to the web.|
|Module Registration||Module register through module.xml file.||Module register through registration.php file||Removed modules folder and module’s declaration is moved to app/code/Vendor_name/Module_name/etc/module.xml.|
|Directories Removed||The downloader, includes, and pkginfo folders are removed.||The generated bin, vendor, and set up folders are in the main directory structure.||downloader as a way to conveniently install the program Magento Connect Manager is removed due to security concerns.|
|File Structure Changes||The folders and files, such as
Are moved to
|Major components are in the vendor folder, and each module’s assets are in the pub folder.||In Magento 2, the structure is not as strict as in Magento 1, so many folders are merged for developers’ ease.|
Magento folder structure is organized logically and hierarchically, with each folder serving a specific purpose in the overall functioning of the Magento application. As long as you know the functions and purpose of folders in the file structure, your components will work properly when you create them. If you’ve any questions about this article, please write in the comments.
What is Magento generated folder?
Magento generated folders are automatically generated by default by Magento application. It contains all the subdirectories and folders that are needed by the system.
What is use of generated folder in Magento 2?
Generated folder in Magento 2 contains numerous content in their subdirectories which includes classes, sessions, cache and much more like generated code, to make sure all methods are correct and properly called.
Where are Magento files stored?
All Magento files and folder are stored in /RootDirectory
Customer Review at
“Great speed, features, knowledgebase, dashboard, UX and fast, expert support. Very happy!”
Stefan [Management Consultant]
Jyotishina is the Magento Community Expert at Cloudways and has 4 years of experience in web development. She has worked on e-commerce sites since the turn of the millennium and was working with Magento before version 1 was released. She loves to travel and explore new ideas whenever she finds time. Get in touch with her at [email protected]