Question

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?

Was it helpful?

Solution

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:

  1. Initial (app/etc/di.xml)
  2. Global (<moduleDir>/etc/di.xml)
  3. Area-specific (<moduleDir>/etc/<area>/di.xml)

During bootstrapping, each application entry point loads the appropriate di.xml files for the requested area.

SOURCE

OTHER TIPS

'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 :

  1. 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.
  2. adminhtml : all dependency applied to admin should be placed at adminthml, so developer can identify it easily.
  3. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top