Magento 2 Development Workshop Learn More

Magento 2 for Beginners

10 lessons

Project Structure Overview

Project Structure Overview

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 project structure before doing any development.

In this lesson we will learn Magento 2 Open Source project structure. It will help you to know files and directories, where to look at every time you work with Magento 2 Open Source project.


Let's have a look at the Magento 2 Open Source project structure.

Magento 2 Open Source Project Structure

First of all, we have to understand that this is a list of files and directories come when you install Magento 2 Open Source application. Not all files are required for development. Moreover, not all files are required to run Magento 2 Open Source application.

There are files you might never open during your development journey. Other files are necessary to change and modify quote 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 which will not be covered in this lesson. During the Mage Mastery education journey and 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 index.php file. This is an entry point of 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 in order to find out how Magento 2 Open Source works and what part of the application processes a HTTP request.

The app directory

The app directory is responsible for storing all configuration files, any themes installed in a Magento 2 Open Source application.

App Directory

Theme 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 in order to provide configuration settings used to create or customize functionality.

The config.php file which provides all available Magento 2 modules which are available for a Magento 2 Open Source application. Each module has a 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 Magento 2 application. An example of such 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 of the directory includes tests of a particular type and additional code which helps to execute tests.

Dev Directory

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.

generated Directory

From time to time, we are going to look into the generated directory in order to understand logic which is used to perform a certain operation.

The lib directory

The directory includes internal and web directories.

lib Directory

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 which can be used for development. It is not recommended to use the router.php as part of Magento 2 Open Source production configuration.

The pub directory

Next directory which comes with Magento 2 Open Source is a pub directory.

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 static.php file which responsible for processing all JavaScript, CSS and HTML files and finding the files in the pub/static directory.

All media content which is going to be uploaded whether via Magento Admin or some programmatically is 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.

setup Directory

It is a second way on how you can install Magento 2 Open Source in addition to command line installation which we have performed in a previous lesson. Usually, this is not a directory we are going to work with.

The var directory

You can find cache of Magento 2 application and 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.

var Directory

Also, the var/log directory where all errors, PHP exceptions will be created and stored.

The vendor directory

This directory includes all 3rd-party dependencies installed. It also include 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 Magento 2 application. Whether we want to install additional Magento 2 module or test the application. During development, this file is going to be used frequently.

Final note

This is what I wanted to show you in this lesson. We are going to work and learn more about each project directory and file during Mage Mastery lessons.

Next Steps

Subscribe to the Mage Mastery newsletter to hear first about new lessons and courses.