Magento Library Organization
Magento 2 organizes code into several library layers, each serving a specific purpose in the architecture.
Library layers:
Magento Framework (
vendor/magento/framework/)- Core libraries: ObjectManager, routing, caching, DB abstraction
- Application lifecycle and bootstrapping
- View system and rendering pipeline
- Event system and plugin architecture
Magento Modules (
vendor/magento/module-{name}/)- Feature modules: Catalog, Customer, Checkout, etc.
- Each module is a separate Composer package
- Contains business logic, models, blocks, controllers
Third-Party Libraries (
vendor/{name}/{package}/)- Composer-installed dependencies
- Zend/Laminas components, Symfony components
- PSR interfaces and implementations
Custom Modules (
app/code/{Vendor}/{Module}/)- Your custom module code
- Not managed by Composer by default
Key framework packages:
vendor/magento/framework/
├── App/ # Application lifecycle
├── Archive/ # File archiving
├── Console/ # CLI commands
├── Data/ # Data utilities
├── DB/ # Database abstraction
├── File/ # File system operations
├── HTTP/ # HTTP handling
├── Interception/ # Plugin system
├── MessageQueue/ # Message queue
├── Module/ # Module management
├── ObjectManager/ # Dependency injection
├── Phrase/ # Translations
├── Relationship/ # Entity relationships
├── Shell/ # Shell commands
├── Stdlib/ # Standard library
├── View/ # View system
└── Webapi/ # Web API framework
Framework Core Components
The Magento Framework provides foundational services that all modules depend on.
Application Bootstrap:
// vendor/magento/framework/App/Bootstrap.php
use Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(
BP,
$_SERVER
);
$bootstrap->run($objectManager->get(
\Magento\Framework\App\Http::class
));
ObjectManager (DI):
// vendor/magento/framework/ObjectManager/ObjectManager.php
$objectManager = $bootstrap->getObjectManager();
$product = $objectManager->create(
\Magento\Catalog\Model\Product::class
);
Event System:
// vendor/magento/framework/Event/Manager.php
$eventManager->dispatch('catalog_product_save_before', [
'product' => $product
]);
Cache System:
// vendor/magento/framework/Cache/Invalidate.php
$cache = $objectManager->get(
\Magento\Framework\Cache\Frontend\Interface::class
);
$cache->clean();
View System:
// vendor/magento/framework/View/Result/Layout.php
$resultPage = $resultPageFactory->create();
$resultPage->getLayout()->addBlock(
\Magento\Framework\View\Element\Text::class,
'custom.text'
)->setText('Hello World');
Framework class mapping:
Magento\Framework\App\Action\Action- Controller base classMagento\Framework\Model\AbstractModel- Model base classMagento\Framework\View\Element\AbstractBlock- Block base classMagento\Framework\Db\Adapter\Pdo\Mysql- Database adapterMagento\Framework\HTTP\Client\Curl- HTTP client
Composer Package Management
Magento 2 uses Composer for dependency management. Core modules are distributed as Composer packages.
composer.json structure:
{
"name": "magento/module-catalog",
"description": "Magento Catalog Module",
"type": "magento2-module",
"version": "103.0.0",
"license": "proprietary",
"require": {
"magento/framework": "103.0.*",
"magento/module-eav": "102.0.*"
},
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\Catalog\\": ""
}
}
}
Package types:
magento2-module- Magento modulesmagento2-theme- Frontend themesmagento2-language- Language packageslibrary- Framework libraries
Autoloading:
Magento uses PSR-4 autoloading for classes and file-based autoloading for registration.php files.
// vendor/autoload.php generated by Composer
require __DIR__ . '/autoload.php';
// magento2-registration.php triggers ComponentRegistrar
require BP . '/app/etc/../../../magento2-registration.php';
Key Composer commands:
# Install dependencies
composer install
# Add a new package
composer require vendor/package:^1.0
# Update dependencies
composer update
# Show installed packages
composer show
# Regenerate autoloader
composer dump-autoload
Navigating Magento Code
When debugging or extending Magento, knowing how to navigate the codebase is essential.
Find a class:
# Using grep to find class definition
grep -r "class ProductRepository" vendor/magento/
# Find file by class name
find vendor/magento -name "ProductRepository.php"
Understand module structure:
vendor/magento/module-catalog/
├── Api/ # Service contracts
├── Block/ # View blocks
├── Controller/ # Request handlers
├── Cron/ # Scheduled tasks
├── Model/ # Business logic
├── Observer/ # Event observers
├── Plugin/ # Interceptors
├── Setup/ # Schema/data installers
├── View/ # Templates, layouts, CSS
├── etc/ # Configuration
│ ├── di.xml
│ ├── events.xml
│ ├── routes.xml
│ ├── webapi.xml
│ └── module.xml
├── composer.json
└── registration.php
Trace code execution:
// Enable class logging in development
debug_backtrace();
// Or use Magento's built-in logging
$this->logger->debug('Entering method X', ['param' => $value]);
Useful debugging tools:
grep -rfor searching across codebase- IDE with Composer autoloading support
- Magento Developer Mode (
bin/magento deploy:mode:set developer) - xdebug for step-through debugging
bin/magento dev:log:tailfor live log streaming
Quiz
1. Where are Magento core framework classes located?
2. What Composer package type is used for Magento modules?
3. How does Magento handle class autoloading?
Flashcards
Question
Where do Magento core modules live?
Click to reveal answer
Answer
vendor/magento/module-{name}/
Question
What file triggers Composer autoloading for a module?
Click to reveal answer
Answer
registration.php listed in the autoload.files array
Question
What package type does Magento use for themes?
Click to reveal answer
Answer
magento2-theme
Question
Where does custom module code go?
Click to reveal answer
Answer
app/code/{Vendor}/{Module}/
Revision Notes
Key Takeaways
- 1. Magento organizes code into Framework, Modules, Vendor packages, and Custom code
- 2. Framework core is in vendor/magento/framework/
- 3. Core modules are in vendor/magento/module-{name}/
- 4. Composer manages dependencies with PSR-4 autoloading
- 5. Custom modules go in app/code/{Vendor}/{Module}/
- 6. Each layer has specific purposes and responsibilities
Interview Tips
- • Describe the different code layers in Magento 2
- • Explain how Composer manages Magento dependencies
- • Know where to find core Magento code when debugging
- • Discuss PSR-4 autoloading in Magento context
- • Explain the difference between vendor/magento and app/code
Cheat Sheet
Libraries Cheat Sheet
Framework: vendor/magento/framework/
Core Modules: vendor/magento/module-{name}/
Custom Code: app/code/{Vendor}/{Module}/
Themes: app/design/frontend/{Vendor}/{Theme}/
Key Framework Classes:
- App/Bootstrap - App lifecycle
- ObjectManager - DI
- View/Element/AbstractBlock - Block base
- Model/AbstractModel - Model base
- Event/Manager - Events
Composer Types: magento2-module, magento2-theme, magento2-language, library