Magento Codebase
Magento 2 is a widely used e-commerce platform, providing extensive built-in features and high-security standards. Its comprehensive functionality ensures an ideal foundation for growing any online business.
In this guide, we’ll cover the basics of Magento 2’s structure, including modules, themes, and more, giving newcomers a solid foundation for understanding the platform’s architecture.
Importance of Folder Structure
Magento’s folder structure is crucial for several reasons:
- It organizes various files and resources, making them easier to manage and maintain.
- It enables the platform to operate smoothly by establishing a clear hierarchy and responsibility for each file.
- It ensures that customizations and extensions can be made without disrupting core functionality.
- It enhances security by isolating sensitive files from publicly accessible resources.
Magento Directory Structure
Magento’s structure divides into three main categories:
- Basic Structure
- Module Structure
- Design Structure
Let’s start with the Basic Structure, which covers standard directories in Magento 2.
Magento Folder Structure
Magento 2’s folder structure has evolved from earlier versions. Its installation root directory holds subdirectories for various predefined features:
- app: Contains core files, configuration, modules, and themes. Custom code resides in
app/code, while theapp/designfolder contains frontend and admin themes. Language packages are stored inapp/i18n, while core Magento modules are separated into thevendordirectory. - bin: Houses the Magento 2 CLI script for module installation, upgrades, reindexing, and more, accessible through SSH.
- dev: Includes developer tools like the Magento Test Framework (MTF) for testing.
- generated: Holds auto-generated code; use
rm -r /generated/*/*to clear and regenerate classes. - lib: Contains libraries outside of the Magento core.
- phpserver: Contains files for the built-in PHP web server (not recommended for security reasons).
- pub: Holds public files, including
index.phpand static site content. It’s recommended as the web root. - setup: Contains database scripts and installation files, including the performance toolkit.
- var: Stores cache, session data, error reports, and other runtime files. It regenerates when
bin/magento setup:di:compileis run. - vendor: Managed by Composer, this directory includes external packages defined in
composer.json.
Areas
Magento has two main “areas”: the adminhtml for the admin panel, and frontend for the storefront. These areas separate backend and frontend components, with unique themes, controllers, and static files for each.
Common Module Directories
Key directories within Magento modules include:
- Block: Contains PHP view classes.
- Controller: Manages incoming requests as part of MVC architecture.
- etc: Holds configuration files, including
module.xml. - Model: Contains model classes as part of MVC architecture.
- Setup: Manages database structure and data setup.
- Observers: Contains event listeners for automated actions.
Additional Directories
Additional module directories may include:
- API: For API-exposed PHP classes.
- Console: For CLI commands.
- Cron: For cron job definitions.
- Helper: For utility functions.
- Plugin: For plugins.
- UI: For UI components and data files.
- view: For static assets, design templates, email templates, and layout files.
Required Files for Modules
Each module must have:
- registration.php: Registers the module with Magento.
- etc/module.xml: Provides basic module information.
- composer.json: Defines module dependencies.
Magento Theme Structure
A typical Magento theme includes directories for configuration (etc), translations (i18n), media previews (media), static assets (web), CSS sources (css/source), fonts, images, and JavaScript files (js).
Language Package Structure
Language packages, named by their locale codes (e.g., en_US), include files such as registration.php to register the package and composer.json for dependency management.
Magento 1 vs. Magento 2 Folder Structure
Magento 2’s file structure has been optimized for better performance and organization compared to Magento 1.x, making development and customization easier and more efficient.
Summary
Magento’s directory structure is logically organized to streamline development, customization, and security. Knowing the purpose of each directory will ensure that your custom components function correctly.
Mohamed Abbas
Technical Lead | Magento Architect