In Magento 2, What case I use di.xml and How to use di.xml for module?

Anyone knew, please show me step by step.


1 Answer 1


What case We use di.xml ?

  • We can use di.xml for ( rewrite ) preference of a particular class.
  • We can send new or replace the existing class arguments.
  • Use plugins to do some stuff before, after and around the function
  • By using virtualTypes creating a sub-class of another class.

Let us take a quick example from Magento 2 customer module.


<preference for="Magento\Customer\Api\AddressRepositoryInterface"
            type="Magento\Customer\Model\ResourceModel\AddressRepository" />

Above code, When someone asks you to instantiate a Magento\Customer\Api\AddressRepositoryInterface it will instantiate a Magento\Customer\Model\ResourceModel\AddressRepository object (the type attribute).

Class preference configuration is not just for interfaces we can change the actual classes as well.

<preference for="Magento\Customer\Model\CustomerManagement"
            type="Magento\Customer\Model\customModel" />

You can create 'customModel' class for 'CustomerManagement' and do the changes. Class preference system as a replacement for the class rewrite system.


2. Arguments

<type name="Magento\Customer\Model\ResourceModel\Group" shared="false">
        <argument name="groupManagement" xsi:type="object">Magento\Customer\Api\GroupManagementInterface\Proxy</argument>

In th above code, We are sending object as an argument, we are saying system to insert "Proxy" class as an object with the name of groupManagement. Also, we can use Arguments for replacing the existing argument too.


3. Plugin

<type name="Magento\Customer\Model\ResourceModel\Visitor">
    <plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />

In the above code , public function clean($object) in visitor class is called after public function afterClean(Visitor $subject, $logResourceModel) which is in Log class.

4 Virtual Types

Creating a virtual type is sort of like creating a sub-class for an existing class.

For more note please go through the practical examples some I mentioned as links from Alan, by practice you can get more clear experience.

Hope this helps.... :)

  • 1
    have you any idea about the set configuration with ifconfig in di.xml for enable/disable module process? if you about that please help me resolve the issue. Nov 17, 2017 at 7:45
  • 1
    Very useful information.
    – Shivam
    Apr 26, 2018 at 12:34
  • 2
    How does someone learn all these stuff? Jul 11, 2018 at 13:23
  • So the <preference> instead of call the class in "for", it call the class in "type" or something? i'm still confusing about this. @@
    – fudu
    Aug 15, 2018 at 3:03
  • 3
    oh nvm, i found this really good answer here. inchoo.net/magento-2/overriding-classes-magento-2 :)
    – fudu
    Aug 15, 2018 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.