Layered Architecture
Magento's Layers
┌─────────────────────────────────────────────────────â”
│ Presentation │
│ (Themes, Blocks, Templates, JavaScript) │
├─────────────────────────────────────────────────────┤
│ Service Layer │
│ (APIs, Interfaces, Business Logic) │
├─────────────────────────────────────────────────────┤
│ Domain │
│ (Entities, Value Objects, Repository) │
├─────────────────────────────────────────────────────┤
│ Infrastructure │
│ (Database, Cache, Search, File System) │
└─────────────────────────────────────────────────────┘
Presentation Layer
Handles UI rendering:
- Themes: HTML, CSS, JavaScript layouts
- Blocks: PHP classes that prepare data for templates
- Templates (.phtml): PHP + HTML for rendering
- Layout XML: Defines page structure and block placement
Service Layer
Business logic and API:
- APIs (Service Contracts): Stable interfaces
- Business Logic: Validation, processing, orchestration
- Plugins/Interceptors: Method-level behavior modification
- Events/Observers: System-wide notifications
Domain Layer
Data models and persistence:
- Entities: Product, Order, Customer
- Repositories: Data access abstraction
- Resource Models: Database operations
- Collections: Query builders
Infrastructure
Technical foundation:
- ObjectManager: Dependency injection container
- Cache: Full-page, block, configuration
- Search: Elasticsearch/OpenSearch
- Queue: Message queue for async processing
Modular Design
How Magento is Modular
Every feature is a module. Even core functionality is modular:
app/code/Magento/
├── Catalog/ # Product catalog
├── Checkout/ # Shopping cart & checkout
├── Customer/ # Customer accounts
├── Sales/ # Orders & invoices
├── Shipping/ # Shipping methods
├── Payment/ # Payment methods
├── CMS/ # Content management
├── Store/ # Store configuration
├── Theme/ # Frontend themes
├── User/ # Admin users & roles
└── ... (100+ modules)
Module Structure
app/code/Vendor/Module/
├── registration.php # Module registration
├── etc/
│ ├── module.xml # Module definition
│ ├── di.xml # Dependency injection
│ ├── events.xml # Event observers
│ ├── frontend/
│ │ └── routes.xml # Frontend routes
│ └── adminhtml/
│ └── routes.xml # Admin routes
├── Api/
│ ├── Data/ # Data interfaces (DTOs)
│ └── *RepositoryInterface.php # Repository interfaces
├── Model/
│ ├── Data/ # Data models
│ ├── *Repository.php # Repository implementation
│ └── ResourceModel/ # Database operations
├── Controller/
│ ├── *Controller.php # Action controllers
│ └── Adminhtml/ # Admin controllers
├── Block/
│ └── *.php # Template blocks
├── view/
│ ├── frontend/
│ │ ├── layout/ # Layout XML
│ │ ├── templates/ # .phtml templates
│ │ └── web/ # CSS, JS, images
│ └── adminhtml/
├── Setup/
│ ├── InstallSchema.php # Database install
│ └── UpgradeSchema.php # Database upgrade
└── Test/
├── Unit/ # Unit tests
├── Integration/ # Integration tests
└── Api/ # API tests
Module Registration
// registration.php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
<!-- etc/module.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
The <sequence> tag defines load order (dependencies).
Areas and Themes
Areas
Magento divides the application into areas for different contexts:
| Area | Purpose |
|---|---|
| frontend | Storefront (customer-facing) |
| adminhtml | Admin panel |
| api_rest | REST API |
| api_graphql | GraphQL API |
| crontab | Cron jobs |
| doc | Documentation |
// Area-specific configuration
// etc/frontend/di.xml — only loaded in frontend area
// etc/adminhtml/di.xml — only loaded in admin area
// etc/di.xml — loaded in all areas
Area Codes
// Area codes are set during bootstrap
use Magento\Framework\App\Area;
Area::AREA_FRONTEND // 'frontend'
Area::AREA_ADMINHTML // 'adminhtml'
Area::AREA_API // 'api'
Area::AREA_CRONTAB // 'crontab'
// Check current area
$areaCode = $this->state->getAreaCode();
Themes
app/design/frontend/Vendor/theme/
├── registration.php
├── theme.xml
├── etc/
│ └── view.xml # Theme configuration
├── Magento_Catalog/ # Module-specific overrides
│ └── templates/
│ └── product/
│ └── list.phtml
├── web/
│ ├── css/
│ │ └── source/
│ │ └── _theme.less
│ ├── js/
│ │ └── main.js
│ └── images/
├── layout/
│ ├── default.xml
│ ├── catalog_product_view.xml
│ └── catalog_category_view.xml
└── templates/
└── header.phtml
Theme Hierarchy
Parent Theme (e.g., Magento/luma)
└── Child Theme (e.g., Vendor/custom)
└── Override specific files
Override order:
1. Child theme files
2. Parent theme files
3. Module default files
Layout XML
<!-- layout/default.xml — applies to all pages -->
<page>
<body>
<referenceContainer name="header">
<block class="Vendor\Module\Block\Logo" name="custom.logo"
template="Vendor_Module::logo.phtml"/>
</referenceContainer>
</body>
</page>
<!-- layout/catalog_product_view.xml — product page only -->
<page>
<body>
<referenceBlock name="product.info">
<block class="Vendor\Module\Block\Product\CustomInfo"
name="custom.product.info"
template="Vendor_Module::product/custom.phtml"/>
</referenceBlock>
</body>
</page>
Layout XML is a powerful system for controlling page structure without modifying PHP code.
Quiz
1. What are Magento's four architectural layers?
2. What is the purpose of 'areas' in Magento?
3. How does Magento achieve extensibility at the architecture level?
Flashcards
Question
What are Magento's four layers?
Click to reveal answer
Answer
Presentation, Service, Domain, Infrastructure
Question
What is an Area in Magento?
Click to reveal answer
Answer
Application context: frontend, adminhtml, api_rest, crontab
Question
How is extensibility achieved?
Click to reveal answer
Answer
Service contracts, plugins, observers, preferences
Question
What is the <sequence> tag in module.xml?
Click to reveal answer
Answer
Defines module load order (dependencies)
Revision Notes
Key Takeaways
- 1. Four layers: Presentation → Service → Domain → Infrastructure
- 2. Every feature is a module with registration.php, etc/module.xml, etc/di.xml
- 3. Areas separate contexts: frontend, adminhtml, API, crontab
- 4. Themes override templates and layout via parent-child hierarchy
- 5. Extensibility via: service contracts, plugins, observers, preferences
Interview Tips
- • Draw the four layers and explain what each handles
- • Explain how modules depend on each other via <sequence>
- • Describe how themes override module templates
Cheat Sheet
Layers:
Presentation → Themes, Blocks, Layout XML
Service → APIs, Business Logic, Plugins
Domain → Entities, Repositories, Collections
Infrastructure → ObjectManager, Cache, Search, DB
Module:
registration.php → ComponentRegistrar::register()
etc/module.xml → Dependencies, version
etc/di.xml → DI configuration
Areas: frontend, adminhtml, api_rest, api_graphql, crontab
Themes: Parent → Child → Override specific files