Knowing and understanding the folder structure can help you quite a bit when customizing codes or installing new extensions. The Module File/Folder structure for Magento is excessively complex which is why we going to cover all high-level files/folders instead of every single one.
1. Magento 2 File Structure
While navigating your root folder, you’ll come across the following files:
Like in the image below.
Each individual folder is made up of subfolders that include files. Let’s go through the contents that come with each file.
This file consists of Magento’s main code, it contains three key sub-directories, namely:
Amongst all of your modules, your customer should be counted in the app/code/<CompanyName>/<ModuleName> directory. You can also find third-party module recommendations here.
The design sub-directory includes sub-folders along with LESS and CSS files. The following two are examples of subfolders included under design.
This subdirectory is a great place to store customized store themes
You can store all admin related themes here.
The i18n sub-directory basically stores language packages. It can be helpful for anyone wanting to establish a store supporting multiple languages.
Here is where you’ll find core codes for Magento, you can develop and edit custom Magento codes. The availability of this sub-directory is based on your installation method. If you have downloaded the GitHub repository there will be an app/code directory.
However, if you aren’t shown a code subfolder in the app folder, don’t worry, your Magento codes can be found in the vendor file. (Vendor file will be discussed in detail below). If you want to dodge any chance of custom and core modules mix up, you might want to keep all Magento core modules separately saved in the vendor directory.
Use composer to store all of your installed packages. You can save your files under app/code/<CompanyName>, rather than working straight out of this folder and avoid any unnecessary overriding while upgrading vendor files.
This subdirectory contains interface preferences and class mapping in di.xml configuration. You can find ACL, configuration, admin and admin menu in this folder.
This sub-directory is a file that you can employ to implement PHP to the built-in server. Please note that, it is not recommended to work in this file to avoid possibly leaving huge security gaps in your website.
This directory includes the Magento file, in other words a CLI script that is executable. With this script you can easily activate certain commands such as disabling/enabling modules, disabling/enabling cache or re-indexing among others.
As the name suggests, this directory consists of all files that are required during setup after installing Magento.
Magneto generated codes are stored under this directory. By default, if you inject a certain class into a constructor Magento will generate a code to make non-existent factory classes.
In this folder you will find sessions, cache, composer_home, di, logs, database backups, view_processed, cached error reports and generated classes. You can produce the contents of various subdirectories including var/di after you run php bin/magento setup:di:compile.
You will ordinarily find system.log and exception.log files under this sub-directory.
This sub-directory is there for all of Magento’s cache. To clear up cache, its recommended to use the php bin/magento cache:clean command to check out development changes.
This folder holds a few tools and Magento 2 test scripts. It stores functional tests programmed to be run by Magento’s Test Framework.
If you are looking for vendor or Magento library files, this file is what you need. It also has all of Magento’s non-module based codes.
You might want to set this to be your web root on your server, because it basically includes all files that are publicly accessible. The pub directory contains security measures to help you restrict public access. Keep in mind that this folder includes static files generated by your Magento theme.
This is a controller stored under the pub directory. It can be used in production mode to run a file.
The framework core exists in this directory, where you will find no problem in installing additional modules. Any operations carried out in these files uses the composer.json file. The vendor file has no files or folders inside instead composer.json creates folders and files for you. Then you’ll be able to view various packages defined by composer.json.
2. Component File Structure
Now that we have covered the basics, let’s take a look at file structures and component types. Magento is designed to search for component-creating configuration files in targeted locations within the component file structure. To make sure the component type performs the correct function you have to
In order to ensure the component type works properly, you need to pursue predefined file structures.
Any component’s root directory will match its name and contain relevant files and sub-directories.
Based on Magento installation, a component’s root directory can be placed in either of the following two locations:
Magento install directory/app
Now this is a logical component development location. You can smoothly create the following environment by cloning the GitHub repository for Magento 2.
- Apply app/code for modules
- Apply app/design/frontend for storefront themes
- Apply app/design/adminhtml for Admin themes
- Apply app/i18n for language packages
- <Magento install directory>/vendor
When you install Magento 2 marketplace and have EE or CE code downloaded as well, this location will be created by the composer. You can also find this location when you extract an archived compressed Magento 2 to install Magento.
This file registers a component with Magento. Its root directory, here, will also be used as the component’s name. Hence, components will, by default, be installed in <Magento root dir>/vendor directory.
This file’s job is to define a component’s basic information such as version numbers or component dependencies. Versions numbers are used in order to conclude which data and schemes should be updated when you run bin/magento setup:upgrade.
This file is needed to determine dependencies required by the component at runtime.
3. Module file structure
You can view a typical module file structure in the image here:
Below are a few common module directories:
Comprises of PHP view classes module logic Model View Controller(MVC) vertical implementation
Comprises of PHP controller classes as a fragment of module logic MVC vertical implementation
Comprises of configuration files; specifically the module.xml, which is needed
Comprises of PHP model classes as a fragment of module logic MVC vertical implementation
Comprises of classes for data setup and module related database structure. During install/ upgrade such data is invoked.
The following folders are great for additional configurations, ancillary functions or other items like plug-ins, localization and layout files.
For PHP classes open to the API
For CLI commands
For cron job definitions
For section files
For aggregated functionality
For files of localization
For files that execute listener commands
For any required plug-ins
For files of data generation
For files that consist of design templates, email templates, static view files and layout files.
4. Theme file structure
You can view a typical theme file structure in the image here:
Below are classic theme directories:
Comprises of configuration files. These include the view.xml file which has image related configurations for all thumbnails and images.
Comprises of translation dictionaries, in the event that there are any
Comprises of theme preview images
Comprises of an optional directory. This optional directory is made up of static files systematized by the following files:
The source file consists of LESS configuration files that invoke global features from Magento’s UI library. It also has a theme.less file that overrules default variables values.
This folder consists of files that override stored UI library files available in lib/web/css/source/lib
Save various fonts to apply to your theme here
Here you can view a number of static images
5. Language package file structure
This is how a directory structure would look like if you needed to operate in the French language:
The top-level directory is the only directory needed for a language pack.
Helpful Tip** to identify the locale, and increase better performance, match the directory name and ISO code.
To summarize, File Structure has a supreme role in tailoring a Magento 2 website. But as long as you are aware of the functions and purpose of folders n the file structure you can be assured that your component file will operate effectively as soon as you create it.