2

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.

Model:

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.

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;
        parent::__construct($context);
     }

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

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

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">
        <arguments>
            <argument name="cart" xsi:type="object">Magento\Checkout\Model\Cart\Proxy</argument>
            <argument name="bulkservice" xsi:type="object">Learn\Pricechange\Model\BulkService\Proxy</argument>
        </arguments>
    </type>
</config>

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

1
  • did you found any answer for this? Commented Mar 31, 2022 at 7:36

1 Answer 1

0

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

https://magento.stackexchange.com/a/296607/70343

1
  • 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
    Commented Nov 29, 2019 at 5:53

Your Answer

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

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