Magento 2: Project Structure Overview
The directory structure of any framework can be challenging to jump on and understand. Magento 2 Open Source is not an exception here and it is important to understand the project structure before doing any development.
In this blog, we will learn the Magento 2 Open Source project structure. It will help you to know files and directories, and where to look every time you work with Magento 2 Open Source project.
Overview
Let’s have a look at the Magento 2 Open Source project structure.
First of all, we have to understand that this is a list of files and directories that come when you install the Magento 2 Open Source application. Not all files are required for the development. Moreover, not all files are required to run the Magento 2 Open Source application.
There are files you might never open during your development journey. Other files are necessary to change and modify quotes frequently.
In this lesson, we will cover all Magento 2 Open Source project files which are on the top level of the project structure. However, there are thousands of files and PHP classes that will not be covered in this lesson. During the Mage Mastery education journey and the next Magento 2 for Beginners lessons, we are going to cover different classes and functionality related to different Magento 2 Open Source modules.
Application Entry Point
The number one file we have to start is the index.php file. This is an entry point for your application. Every time Magento 2 Open Source application receives an incoming HTTP request it is processed by the index.php file. Depending on your server document root configuration, you may also configure a similar index.php file located in the pub/directory.
For security reasons, it is recommended to configure the document root of a Magento 2 Open Source application to point to the pub/directory.
All your debugging activities should start with the index.php file. From this file, you can go deeper into the application execution process to find out how Magento 2 Open Source works and what part of the application processes an HTTP request.
The app directory
The app directory is responsible for storing all configuration files, and any themes installed in a Magento 2 Open Source application.
These files include PHTML, HTML, CSS, LESS, JavaScript, and images. Also, inside the app directory, you can find source code files. There are currently no files in the code directory as of an empty Magento 2 Open Source installation. Usually, all additional customizations happen inside the app/code directory.
The app/etc directory holds all default configuration settings for a Magento 2 Open Source application. Upon the application installation the env.php file. This is a primary file where you can find database connection settings, cache-related settings, and other settings which are going to be covered in the next lessons.
Magento 2 platform uses XML files for storing configuring settings and dependency injection (Service Container) configuration. The di.xml file located in the app/etc directory is a good example of dependency injection configuration. We are going to discuss different types of XML files to provide configuration settings used to create or customize functionality.
The config.php file provides all available Magento 2 modules that are available for a Magento 2 Open Source application. Each module has an active/disabled status allowing Magento 2 Open Source to include a module’s functionality.
The bin directory
The bin directory provides a Magento CLI (Command Line Interface) tool. It provides an interface via CLI to different Magento 2 commands to perform changes in the Magento 2 application. An example of such a command can be a cache clean or compilation.
The dev directory
The dev directory holds all tests which come together with Magento 2 Open Source.
Magento 2 includes 8 different types of tests inside dev/tests directory. Each directory includes tests of a particular type and additional code which helps to execute tests.
The dev/tools directory provides different tools and its configuration such as Grunt and other non-commonly used files.
The generated directory
This directory holds all auto-generated PHP classes. Magento 2 relies on PHP code generation.
From time to time, we are going to look into the generated directory to understand the logic that is used to perform a certain operation.
The lib directory
The directory includes internal and web directories.
The internal directory is not something we are going to work with. And the web directory provides frontend or web-related files. It includes JavaScript 3rd-party libraries such as jQuery, KnockoutJS, RequireJS, and other libraries. Also, the web includes CSS/LESS files as well as UI documentation.
The phpserver directory
It provides a simple PHP server that can be used for development. It is not recommended to use the router.php as part of the Magento 2 Open Source production configuration.
The pub directory
The next directory that comes with Magento 2 Open Source is a pub directory.
This is a server root directory of a Magento 2 application. The index.php is a primary file responsible for processing HTTP requests. There is a static.php file which is responsible for processing all JavaScript, CSS, and HTML files and finding the files in the pub/static directory.
All media content will be uploaded via Magento Admin or programmatically stored in the pub/media directory. Every time you would have to check the result of Import execution or want to look up a media file you have to check this directory.
The setup directory
This directory is used to provide a Web Setup Wizard and other setup-related scripts.
It is a second way how you can install Magento 2 Open Source in addition to command-line installation. Usually, this is not a directory we are going to work with.
The var directory
You can find a cache of Magento 2 applications and a session directory. In case your configuration says that all sessions have to be stored in a filesystem, the var/session directory will hold all user session files.
Also, the var/log directory where all errors and PHP exceptions will be created and stored.
The vendor directory
This directory includes all 3rd-party dependencies installed. It also includes Magento 2 source files under the vendor/magento directory. We are going to look into the directory quite often during development.
Other files
Magento 2 Open Source provides different sample configuration files for Apache server and Nginx. For example files grunt-config.json.sample, Gruntfile.js.sample, and packages.json are responsible for installing frontend/JavaScript dependencies.
The last one is the composer.json file. It provides all dependencies in the Magento 2 application. Whether we want to install an additional Magento 2 module or test the application. During development, this file is going to be used frequently.
If you want to learn more about Magento 2, make sure to check the Magento 2: From Basics to Advanced Development online course.