12

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.

56

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.

1.Preference

<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.

http://alanstorm.com/magento_2_object_manager_preferences

2. Arguments

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

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.

http://alanstorm.com/magento_2_object_manager_argument_replacement

3. Plugin

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

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.... :)

| improve this answer | |
  • 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. – Kartik Asodariya Nov 17 '17 at 7:45
  • Very useful information. – Shivam Apr 26 '18 at 12:34
  • 2
    How does someone learn all these stuff? – Mohammed Joraid Jul 11 '18 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 '18 at 3:03
  • 3
    oh nvm, i found this really good answer here. inchoo.net/magento-2/overriding-classes-magento-2 :) – fudu Aug 15 '18 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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