Skip to content
beginner Phase 19 · Magento Directory Structure

Magento 2 Directory Structure

Complete Magento directory structure - app/code, app/design, vendor, pub, var, generated, dev, bin, setup.

45m
0 problems
Topic Progress 0%

Top-Level Directory Structure

Magento 2 follows a specific directory structure that organizes code, configuration, and runtime files.

magento2/
├── app/                 # Application code and configuration
│   ├── code/            # Custom modules
│   ├── design/          # Custom themes
│   ├── etc/             # Global configuration
│   └── i18n/            # Translation files
├── bin/                 # CLI tools
│   └── magento          # Main CLI entry point
├── dev/                 # Development tools
│   ├── tests/           # Integration tests
│   └── tools/           # Dev utilities
├── generated/           # Auto-generated code
│   ├── code/            # Factories, proxies, interceptors
│   └── metadata/        # Compiled DI configuration
├── lib/                 # Internal libraries
│   ├── internal/        # Magento internal libs
│   └── web/             # Base frontend assets
├── pub/                 # Web root (DocumentRoot)
│   ├── index.php        # Entry point
│   ├── static/          # Static assets (CSS, JS, images)
│   └── media/           # User-uploaded media
├── setup/               # Installation and upgrade scripts
├── var/                 # Runtime files (cache, logs, sessions)
│   ├── cache/           # Cache files
│   ├── log/             # Log files
│   ├── session/         # Session files
│   ├── view_preprocessed/ # Preprocessed CSS/JS
│   └── composer_home/   # Composer home directory
├── vendor/              # Composer dependencies
│   ├── autoload.php     # Composer autoloader
│   ├── magento/         # Core Magento packages
│   └── ...              # Third-party packages
├── composer.json        # Composer configuration
├── composer.lock        # Locked dependency versions
├── Gruntfile.js         # Frontend build tools
├── package.json         # Node.js dependencies
└── .htaccess            # Apache configuration

Key directories:

  • app/ - Your custom code lives here
  • vendor/ - Core Magento and Composer packages
  • pub/ - Web-accessible directory (DocumentRoot)
  • generated/ - Never edit manually (auto-generated)
  • var/ - Runtime files (cache, logs, sessions)

app/ Directory Deep Dive

The app/ directory contains your custom code and application configuration.

app/
├── code/                    # Custom modules
│   └── Vendor/
│       └── Module/
│           ├── registration.php
│           ├── etc/
│           ├── Model/
│           ├── Block/
│           ├── Controller/
│           └── ...
├── design/                  # Custom themes
│   ├── frontend/
│   │   └── Vendor/
│   │       └── theme/
│   └── adminhtml/
│       └── Vendor/
│           └── theme/
├── etc/                     # Global configuration
│   ├── config.php           # Enabled modules list
│   ├── env.php              # Environment configuration
│   ├── di.xml               # Global DI configuration
│   └── non Composer Component List.php
└── i18n/                    # Translation CSV files
    ├── en_US.csv
    └── fr_FR.csv

app/code/ - Custom modules:

# Create new module directory
mkdir -p app/code/Vendor/Module/etc
mkdir -p app/code/Vendor/Module/Model
mkdir -p app/code/Vendor/Module/Block
mkdir -p app/code/Vendor/Module/Controller

app/etc/ - Configuration:

// app/etc/env.php - Environment-specific settings
return [
    'db' => [...],
    'cache' => [...],
    'session' => [...],
    'crypt' => [...],
    'install' => ['date' => '...'],
];

app/i18n/ - Translations:

"Module::String to translate","Translated string"
"Vendor_Module::Hello World","Bonjour le Monde"

pub/ and vendor/ Directories

pub/ - Web Root (DocumentRoot):
The pub directory is the only web-accessible directory. Your web server's DocumentRoot should point here.

pub/
├── index.php              # Application entry point
├── .htaccess              # Apache URL rewriting
├── static/                # Static frontend assets
│   ├── frontend/          # Theme-specific CSS/JS
│   ├── adminhtml/         # Admin static files
│   ├── _cache/            # Static file cache
│   └── deploy_versioned.js
├── media/                 # User-uploaded files
│   ├── catalog/           # Product images
│   ├── customer/          # Customer avatars
│   └── tmp/               # Temporary uploads
├── errors/                # Error page templates
└── app/                   # Symlink to app/ (for compatibility)

Security: Only pub/ should be publicly accessible. All other directories contain sensitive code.

vendor/ - Composer Dependencies:

vendor/
├── autoload.php           # Composer autoloader entry point
├── composer/              # Composer internals
│   ├── autoload_classmap.php
│   ├── autoload_namespaces.php
│   └── installed.json
├── magento/               # Core Magento packages
│   ├── framework/         # Magento Framework
│   ├── module-catalog/    # Catalog module
│   ├── module-customer/   # Customer module
│   ├── module-checkout/   # Checkout module
│   └── ...                # All core modules
├── laminas/               # Laminas (Zend) components
├── symfony/               # Symfony components
└── psr/                   # PSR interfaces

Vendor directory structure per module:

vendor/magento/module-catalog/
├── Api/                   # Service contracts
├── Block/                 # View blocks
├── Controller/            # Controllers
├── Model/                 # Business logic
├── etc/                   # Configuration files
├── view/                  # Templates, layouts, CSS/JS
├── composer.json          # Package definition
└── registration.php       # Module registration

var/ and generated/ Directories

var/ - Runtime Files:
Contains temporary files that change during application execution.

var/
├── cache/                 # Application cache
│   ├── mage--*            # Cache frontend storage
│   └── page_cache/        # Full page cache
├── log/                   # Application logs
│   ├── system.log         # System messages
│   ├── exception.log      # Exception traces
│   ├── debug.log          # Debug messages
│   └── cron.log           # Cron job output
├── session/               # Session files (file-based)
├── view_preprocessed/     # Preprocessed CSS/JS
├── compilation/           # Compilation output
├── di/                    # Compiled DI config
├── composer_home/         # Composer home directory
└── report/                # Error reports

generated/ - Auto-Generated Code:
Never edit files in this directory manually. They are regenerated by setup:di:compile.

generated/
├── code/                  # Generated classes
│   ├── Magento/
│   │   └── Catalog/
│   │       └── Model/
│   │           ├── ProductFactory.php
│   │           ├── Product\Interceptor.php
│   │           └── ...
│   └── Vendor/
│       └── Module/
│           └── Proxy/
│               └── SomeClassProxy.php
└── metadata/              # Compiled DI configuration
    ├── global.php         # Global DI config
    ├── frontend.php       # Frontend DI config
    ├── adminhtml.php      # Admin DI config
    └── crontab.php        # Cron DI config

bin/ - CLI Tools:

bin/
└── magento                # Main CLI entry point

Key CLI commands:

php bin/magento setup:install    # Install Magento
php bin/magento setup:upgrade    # Run upgrades
php bin/magento setup:di:compile # Compile DI
php bin/magento cache:flush      # Clear caches
php bin/magento module:enable    # Enable modules

Quiz

1. Which directory should be set as the web server DocumentRoot?

Question 1 options

2. Where do custom modules live?

Question 2 options

3. Which directory should never be edited manually?

Question 3 options

Flashcards

Question

What is the DocumentRoot for Magento?

Answer

pub/

Question

Where are core Magento modules located?

Answer

vendor/magento/

Question

What does bin/magento provide?

Answer

CLI commands for managing Magento

Question

Where are compiled DI metadata files stored?

Answer

generated/metadata/

Revision Notes

Key Takeaways

  • 1. pub/ is the DocumentRoot - only web-accessible directory
  • 2. app/code/ contains custom modules
  • 3. vendor/magento/ contains core Magento packages
  • 4. generated/ is auto-generated - never edit manually
  • 5. var/ contains runtime files (cache, logs, sessions)
  • 6. bin/magento provides CLI management tools

Interview Tips

  • List all top-level Magento directories and their purposes
  • Explain why pub/ is the DocumentRoot
  • Know where to find custom vs core code
  • Describe what happens in generated/ during compilation
  • Discuss the security implications of directory structure

Cheat Sheet

Directory Structure Cheat Sheet

app/           # Custom code and config
  code/        # Custom modules
  design/      # Custom themes
  etc/         # Global config (env.php, config.php)
bin/           # CLI tools (bin/magento)
generated/     # Auto-generated (factories, proxies)
lib/           # Internal libraries
pub/           # DocumentRoot (web-accessible)
  static/      # CSS/JS/images
  media/       # User uploads
setup/         # Installation scripts
var/           # Runtime (cache, logs, sessions)
vendor/        # Composer dependencies
  magento/     # Core modules and framework