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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.Sign up to join this community
What case We use di.xml ?
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.
<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
Also, we can use Arguments for replacing the existing argument too.
<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.... :)