In this part of our article we are going to overlook:
- Layout extending and overriding
- Modules templates
- Theme removal
In this article we are going to dive into Magento’s module-based architecture description and MVVM architecture. You will learn the fundamentals of each of the 4 layers of Magento 2 architecture. Understanding layered software pattern is essential for understanding basic Magento product organization. Below you can see the list of questions answered in the article.
Modules in Magento 2 are implemented by means of MVVM architecture.
MVVM is divided into 3 layers:
According to the official documentation, Magento 2 architecture is split into 4 layers.
Presentation Layer is the upper layer. It contains all the View elements (including layouts, blocks, templates, css, js) and controllers.
Presentation Layer usually calls service layer using service contracts. But, depending on the implementation, it may overlap with business logic.
Service layer is an interlayer between presentation layer and domain layer. It implements service contracts, which are defined using PHP interfaces. Service contracts allow to add or change business logic resource model using dependency injection file (di.xml). Service layer is also used for granting access to API (REST/SOAP or other modules).
Service interface is declared in /Api namespace of the module.
Data (entity) interface is declared in /Api/Data. Data entities are data structures passed to and returned from service interfaces.
Domain layer is responsible for the business logic, which does not contain resource-specific or database-specific information. Domain layer can also include service contracts.
Each data model at the level of domain layer depends on the resource model, which is responsible for accessing the database.
Persistence layer describes resource model, which is responsible for extracting and modifying data in the database using CRUD requests.
Additional business logic capabilities are also implemented here, for example, data validation and database functions implementation.
Some modules may conflict with each other if the dependencies in module.xml (sequence) are incorrectly indicated.
Not all standard classes can be redefined using modules.
Modules interact with each other using Dependency injection (di.xml) and layout files (/view/frontend/layout, /view/adminhtml/layout). They can also depend on other modules if they use their logic.
If modules use Dependency injection, they can load in the wrong order or try to redefine the already redefined functionality of the module. To solve this problem, you should use sequence in module.xml.
If the modules use other modules’ logic and the module, whose logic is used, is disabled, dependencies will not be used and an error will occur when executing the code.
That’s it concerning Magento 2 module-based architecture, we hope you found it useful. By the way, we have an article, in which we compared the architecture of the two Magento versions, click here to check it out.