Learn about Magento 2 File Structure Quickly

Posted by

In this article, we will discuss the Magento 2 file structure. When you install Magento that time the directory structure is not the same after you run the composer install command. There are more directories added after you run the command. Understanding the Magento 2 file structure is important for developers to create, customize, and maintain a Magento 2 store effectively.

Overview of the Magento 2 File Structure

The structure is designed in such a way that it keeps custom code in a certain directory rather than mixing it with core files. Knowing and understanding the folder structure can help you when you are customizing codes or installing new extensions. There are 3 file structures, they are Basic Structure, Module Structure and Design Structure. We will be looking into Basic Structure in this article.

The root directory of Magento after installation will have the following directory in it.

app

As a developer, you will mostly interact with this directory because all the custom and 3rd party code will be in this directory. This will have all 3rd party and custom modules and themes. There are 3 directories under app, they are app/code, app/design and app/etc. There are also 2 more files autoload.php which is responsible for autoloading classes and bootstrap.php which is used for initializing your application.

app/code

This directory won’t be there when you do a fresh installation. You will need to create this directory. This directory will hold all of your custom Magento 2 modules. As a developer, you will need to work in this directory. It contains modules and each module has its file structure which will be covered in a different article. Like, create a custom module.

app/design

If you are a frontend developer or a theme developer then this is the directory you will need to work. Inside the app/design, there are 2 more directories adminhtml and frontend. Your custom theme for admin will be created inside adminhtml and the end user theme will be created inside frontend. The theme has its file structure which will be covered in a different article.

app/etc

This sub-folder contains global configuration files for the entire application, such as env.php and config.php.

  • env.php:- The env.php has important settings such as admin path, database connection, page cache, and crypt key which is used to protect sensitive data like passwords and credit card details.
  • config.php:- On the other hand, config.php has a list of all the module cores, 3rd party and custom. Each will have a value of either 1 or 0 marking it as active or disabled.
  • di.xml:- There is also a di.xml file which contains class mapping and interface preferences globally.
  • db_schema.xml:- It is used for patching database tables.
  • NonComposerComponentRegistration.php:- By name itself says its purpose. To register the module and theme which are in the app directory.
  • registration_globlist.php:- It returns an array which has a path to each module, theme and translation.
  • vendor_path.php:- It has a directory name vendor which is returned when being called.

bin

The bin directory contains only 1 file named magento.php this file has a Magento CLI executable script. This file is responsible for the command you run in CLI. Like if you clear the cache you run php bin/magento cache:clear. This is the file which takes your command passes it to the respective code and shows you output on CLI.

dev

This directory won’t be used much. It contains automated functional tests and a few other tools such as grunt are stored here.

generated

Magento 2 uses the generated directory to store auto-generated classes, proxies, and other performance-related files such as the creation of non-existent factory classes if the class is injected in a constructor. This directory helps in optimizing the execution of the Magento application. This is the directory which gets populated when you run the command php bin/magento setup:static-content:deploy.

lib

The lib directory holds Magento-specific libraries and third-party dependencies not managed by Composer. Key subdirectories include internal and web. The internal directory holds Magento’s internal libraries where whereas the web directory has JavaScript libraries and resources.

phpserver

The phpserver folder is one which you should not mess around with as it has a file router.php. It is a router file for the php Built-in web server. It is recommended not to work on this file as it would lead to potential big security holes.

pub

The pub folder is a short form of a publicly accessible directory. Being only publicly accessible provides a security measure to restrict the root directory. When your Magento 2 application is running in production mode that time it is accessed from index.php file. The generated files of the themes are also under this directory. There are sub-directories under it.

static: Contains static files like CSS, JavaScript.

media: Houses media files like images and videos.

errors: Custom error pages and existing once such as 404.

Setup

When you are installing Magento 2 platform this is the directory responsible for it. It contains the setup files and resources needed for the installation of Magento. It also has several important packages, such as the performance toolkit and database scripts.

var

The var folder contains raw sessions, cache, database backups, logs and cached error reports. It has a sub directory which does each of these jobs.

cache: Caching mechanisms to improve performance.

log: Log files for debugging and monitoring such as system.log and exception.log

session: Session data for user interactions.

view_preprocessed: Pre-processed view files for performance optimization.

vendor

The last directory of the Magento 2 file structure is the vendor directory. This directory not only has a Magento 2 core module but also has 3rd party module which was installed via composer. When you update your Magento store to a newer version this folder gets updated. Hence it is highly recommended not to make any changes in the file as after an update all these changes will vanish.

To make any modification for the vendor module, either override those files or create plugin classes inside your custom module which will reside in the app/code. The same goes for theme if you want to make changes to a theme then you need to create a child theme for it under app/design.

Other files in Magento 2

There are other files like composer.json which has all the details of packages and the authentication to download those packages from the respective location. If you brought a new module from the marketplace you can either install it inside the app/code or add the package detail here in composer.json. The advantage of doing it with the composer helps when getting a new update by just executing the composer command.

Apart from this, there are .htaccess for server settings and auth.json for downloading packages in case you are using the cloud version of Adobe Commerce. The auth.json will have public and private keys as username and password respectively for repo.magento.com.