I am learning the Magento 2 proxy concept so that I followed the https://webkul.com/blog/magento2-proxy-design-pattern-code-generation/ blog and discovered the proxy concept.

I am a bit clear about the proxy theory, But I want to implement in real-time so that I followed the below procedure to learn the practical.


namespace Learn\Pricechange\Model;
class BulkService {

    public function __construct() {
        echo 'i am in bulk service instantiated'."<br/>";

I am going to use this as a proxy in my controller.


namespace Learn\Pricechange\Controller\Index;
use Learn\Pricechange\Model\BulkService;
class Change extends \Magento\Framework\App\Action\Action

    protected $_bulkService;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        BulkService $bulkService,
        array $data = []
     ) {
        $this->_bulkService = $bulkService;

    public function execute(){
            echo 'bulkservice class is not instantiated';

Then as per the proxy concept, I included the required code in frontend/di.xml


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Learn\Pricechange\Controller\Index\Change">
            <argument name="cart" xsi:type="object">Magento\Checkout\Model\Cart\Proxy</argument>
            <argument name="bulkservice" xsi:type="object">Learn\Pricechange\Model\BulkService\Proxy</argument>

so as per the above code when you hit the frontend, it should not instantiate the BulkService and do not print the constructor function value(i am in bulk service instantiated).

But in my case it's getting instantiated first and print the constructor function values(i am in bulk service instantiated) after that it's print the execute function value.

So i am bit confused this logic, I am looking forward the better support.

enter image description here


I think you are asking for example where a proxy is used but not injected via constructor


  • My question is, if you added any class in the di.xml(<argument name="bulkservice" xsi:type="object">Learn\Pricechange\Model\BulkService\Proxy</argument>) as a proxy, it'll not instantiated, until call any function in that proxy class. but in my case also I didn't call any function of the proxy class, but it has instantiated – senthil Nov 29 '19 at 5:53

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.