Hello Magento Friends,

In this blog article, we will learn about What is Proxies in Magento 2, why it is needed, and how to use Proxy in Magento 2.

What is Proxies in Magento 2?

Proxies are used to solve a specific problem in Magento 2. Proxies work as a substitute. In programming, proxies are classes that can be used in place of any other classes. In Magento 2 Proxies are used on behalf of resource hungry classes.

Why Proxies are used in Magento 2?

Magento 2 uses various types of dependencies of which constructor injection and method injection are the best. But there is trouble using the constructor injection.

If you wish to use another class object in your class you are unable to instantiate the class in Magento 2. Instead, you need to inject the object in the constructor and afterward, it can be used in the class. Doing so will instantiate all the dependencies injected in your class constructor when your class is instantiated. This slows down the process. To overcome this, a proxy design pattern is used.

How to use Proxy in Magento 2?

Let’s take an example to know how Proxy is used in Magento 2.

Step 1: Add a type configuration in the di.xml

Step 2: After that open terminal and execute the below command in the Magento root path

Then the following classes will be generated automatically at the given path :

magento_root_path\generated\code\Magento\Catalog\Model\Product\Attribute\Source\Status\Proxy.php

magento_root_path\generated\Magento\Catalog\Model\Product\Link\Proxy.php

Final Words:

Hence, this was all about Proxy in Magento 2. Share your doubts in the comment section. Hire a Magento Developer that can make your work easy. Stay in touch with us for further blogs.

Happy Reading!

Click to rate this post!
[Total: 5 Average: 5]