Understanding Magento 2 Folder Structure

Make your transition from Magento 1 to Magento 2 easier by learning what the different folders contain to enable you easily customize your e-commerce site.

In this article, I’ll take you through the Magento 2 folder structure and how to navigate through it. If you are coming from Magento 1, knowing where to add your modules or adding a custom theme to change your store appearance can be daunting at first.

After a clean install of Magento 2, you will have a folder structure similar to this. Download any Magento version from this link and get the setup instructions from here.

Now let’s go through what each folder contains and it’s file contents.

app/

This folder contains three sub-directories mainly design, i18n and etc by default. The app/code/<CompanyName>/<ModuleName> directory should contain all the custom modules that you have created. It is also recommended to store all your third party modules here.

Your custom store theme should be stored under app/design/frontend subdirectory while the app/design/adminhtml subdirectory should be used to store all your Admin themes.

app/i18n directory stores language packages. You might want to use this folder if you want to create a multi-language store to suit your customer needs.

The code subdirectory will be missing after a clean install. All Magento core modules are in the vendor directory in order to avoid the mixing of your custom modules with the Magento core modules. Unlike in Magento 1 where one had to commit all their Magento core modules to their Git repository, in Magento 2 the two are separated.

Stores all your packages installed via Composer including Magento core modules. Working from this folder is not advisable since all your changes will be overridden once you upgrade any of your Magento vendor files. Extending the modules under app/code/<CompanyName> or app/design is highly recommended.

phpserver/

This folder contains the Router.php file whose main purpose is to implement the PHP built-in server. Working with this file is discouraged because it has tons of potentially big security holes. See the READMe

bin/

This directory contains the Magento CLI executable script. This script activates Magento commands that help in clearing cache, reindexing etc.

setup/

Contains Magento’s installation setup files.

generated/

Stores all of Magento’s generated code. By default, Magento generates code to create non-existent factory classes if the class is injected in a constructor.

var/

Contains generated classes, cache, sessions, database backups, and cached error reports.This folder has a number of sub directories including var/di whose contents are generated once you run php bin/magento setup:di:compile.

var/log contains magento log files mainly exception.log and system.log files.

var/cache stores all of Magento’s cache. In order to see your developement changes, it is advisable to continuously clear your cache using this command php bin/magento cache:clean

dev/

Contains automated functional tests that the Magento Test Framework runs.

lib/

Stores all Magento and vendor library files. It also contains all the non-module based Magento code. This is mostly system code that aid Magento to run.

pub/

This directory also contains anindex.php file that is used to run the application in production mode. The pub directory offers a security measure whereby public access to the root directory asset is restricted. This folder also contains generated static files of your Magento theme.


Conclusion

At this point, I believe you’ve understood the Magento 2 folder structure and what content goes in each folder. This will help you get started with Magento 2 and easily customize your store within a short period of time. You can always refer to the official Magento documentation to get a deeper understanding of how different Magento components interact with each other.

If you’ve found this article useful, reach over to the 👏 button and hit it as many times as you have enjoyed reading this post. This will certainly go a long way. Feel free to drop a comment/suggestion. You can also find me on twitter or LinkedIn. Happy hacking!