---
# System prepended metadata

title: Magento 2.4 Introduction
tags: [magento]

---

---
tags: magento
GA: UA-167433668-1
title: Magento 2.4 Introduction
description: Magento 2.4 architecture, request flow, etc.
image: https://static.magento.com/sites/default/files8/2020-08/21998_dx_Commercial%20Social%20Share%20Image%20-%20Magento.jpg
---

**Content:**

[TOC]

---

ref: [Magento Website](https://magento.com/), [Magento Doc](https://devdocs.magento.com/)

- A content management system, much like Wordpress. 
- Specialized for e-commerce.
- Very robust, more complex than Wordpress.
- Has component (plugin/extension) marketplace eco-system.
- Components
    - Themes: used to modify appearance
    - Module: used to modify/extend behaviour
    - Language: used for translation

Architecture
---

ref: [Architectural Overview](https://devdocs.magento.com/guides/v2.4/architecture/archi_perspectives/ALayers_intro.html)

[![Architecture diagram of magento 2.4](https://devdocs.magento.com/common/images/archi_diagram_desired-state.png)](https://devdocs.magento.com/guides/v2.4/architecture/archi_perspectives/arch_diagrams.html)


Layered architecture enables separation of presentation and business logic.

### Presentation Layer

Consists of:
- view elements
    - layout
    - block
    - templates
- controller

HTML is generated from a tree of view elements. These view elements could be:
- Blocks
    - dynamic content
    - can contain named child view elements held in `as` attribute 
- Containers
    - *ordered* group of children view elements

> **The browser forms a product web page by asking the view element tree to render itself into HTML.** Containers and blocks emit HTML that encloses their children appropriately. Blocks can generate their content using static HTML, Knockout JS scripts, and PHTML.
> -- [Presentation Layer | Magento 2 Dev Doc](https://devdocs.magento.com/guides/v2.4/architecture/archi_perspectives/present_layer.html)

Themes
- are presentation layer components
- resides in a unique directory
- contains custom page layouts, templates, skins, and language files

Inter-layer interation
> Web users interact with components of the **presentation layer** to select actions that initiate calls to the underlying layers. **Presentation layer components** make calls to the **service layer**, which in turn sends requests to the **domain layer**.
> -- [Presentation Layer | Magento 2 Dev Doc](https://devdocs.magento.com/guides/v2.4/architecture/archi_perspectives/present_layer.html)

APIs
- REST
- SOAP
- GraphQL

### Service Layer

- contains *service contracts*, which defines the behaviour of the implementation
- provides access to REST/SOAP API (by binding service contracts to the APIs)
- provides API for others modules

Service contract clients:
- controllers of Presentation Layer
- web services (from external apps via REST/SOAP API calls)
- other modules through service contracts

Service contract is defined by:
- service interfaces (in the `/API` directory, example: [Catalog API :arrow_upper_right:](https://github.com/magento/magento2/blob/2.4/app/code/Magento/Catalog/Api))
- data interface (in the `API/Data` directory, example: [Catalog API/Data :arrow_upper_right:](https://github.com/magento/magento2/blob/2.4/app/code/Magento/Catalog/Api/Data)), contains getters and setters for entity table entries and extension attributes.

Service contracts provide:
- repository interfaces
- management interfaces
- metadata interfaces

### Domain Layer

Holds **business logic** that defines operations that can be performed on particular types of data. Typically does not contain resource-specific / database-specific information. 

- defines **generic** Magento data objects / models that contain business logic.
- (optional) implements service contracts

#### Model

**Domain-layer model** refereces **resource model**, which is used to retrieve data from database. Database logic (i.e. MySQL) is stored in resource model. 

#### Access

- loosely coupled (recommended): using service contracts for one module to access another's domain layer.
- tightly coupled (not recommended, but sometimes necessary): direct call from another module
- plugging: event hooks, plugins, `di.xml` (dependency injection)

### Persistence Layer

Uses active record pattern strategy. Model object contains *resource model* that maps to database rows. Resource model's functions include:
- executing CRUD requests 
- additional business logic, e.g. data validation

#### collection

A special resource model, implemented as a *class*, usually used for returning multiple items from a query. Similar to a SQL `WHERE` clause.

#### Entity-Attribute-Value (EAV) models

A resource model that uses EAV resource has its attributes spread out over multiple MySQL tables, e.g. `Customer`, `Catalog`, `Order`.

Request Flow
---
ref: [dckap](https://www.dckap.com/blog/request-flow-in-magento-2/), [magento stackexchange](https://magento.stackexchange.com/questions/72923/request-flow-of-magento-2)

- Entry point: `/index.php`
    1. `Bootstrap::createApplication()`
        - `ObjectManagerFactory::create()` creates app & return it to `index.php`
    2. `Bootstrap::run()`
        - `Application::launch()`
            1. finds out which area (admin/frontend) should be loaded
            2. load the area's config
            3. calls `Magento\Framework\App\FrontControllerInterface::dispatch()` of the area
                1. iterate through routes to match request
                2. matched route returns an instance of `ActionController`
                3. `FrontController` calls `ActionController::dispatch()`
                    - `ActionController` performs some tasks (e.g. `Action::execute()`) and returns `ResultInterface` to `FrontController`
                4. `FrontController` returns `ResultInterface` to `Application::launch()`
            4. renders the `ResultInterface`
---

Installing Magento 2.0: [Installation](https://hackmd.io/@rgbkoi/Hyj-HHEyO)