What's the difference between di.xml in the different etc folders?
-
04-10-2020 - |
سؤال
Looking at the core Magento2 modules in the vendor folder, I notice that there are di.xml
placed in different folders etc/adminhtml/di.xml
, etc/frontend/di.xml
and etc/di.xml
.
The Magento_Cms
module itself has di.xml
in all 3 different folders!
What's the effect of placing the di.xml
in the 3 different folders? How can I know when I should place my di.xml
in which folder?
المحلول
Each module can have a global and area-specific di.xml
file. Magento reads all the di.xml
configuration files declared in the system and merges them all together by appending all nodes.
As a general rule, the area specific di.xml
files should configure dependencies for the presentation layer, and your module’s global di.xml
file should configure the remaining dependencies.
Magento loads The configuration in the following stages:
- Initial (
app/etc/di.xml
) - Global (
<moduleDir>/etc/di.xml
) - Area-specific (
<moduleDir>/etc/<area>/di.xml
)
During bootstrapping, each application entry point loads the appropriate di.xml
files for the requested area.
نصائح أخرى
'di.xml' is used for set dependency.
You can place it in etc folder or frontend or adminhtml folder
but purpose to set it in different folder is :
- frontend : all dependency which apply for frontend is needed to set in it, so developer can have clear idea about dependency applied for frontend development.
- adminhtml : all dependency applied to admin should be placed at adminthml, so developer can identify it easily.
- etc : Its for general dependency, so we can define dependency which can be used in both frontend as well as backend.
Different folder created for different dependency can make it easy to find any dependency applied on Backend or frontend or both.