8

In Magento 1 I can do the following:

$response = $observer->getEvent()->getControllerAction()->getResponse();
if(!$response) {
   return $this;
 }

$html = $response->getBody(); 

How can I do the same in Magento 2?

6
+50

Assuming your module is under Vendor\Module

If you want to captures the HTML from the response, you can declare your event under etc\<area>\events.xml (where <area> is adminhtml or frontend ):

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_front_send_response_before">
        <observer name="vendor_module" instance="Vendor\Module\Model\Observer" shared="false" />
    </event>
</config>

Then under Model\Observer.php:

<?php

namespace Vendor\Module\Model;

use Magento\Framework\Event\ObserverInterface;

class Observer implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
        $response = $observer->getEvent()->getData('response');
        if (!$response) return;
        $body = $response->getBody();
    }
}
|improve this answer|||||
  • Yes it's working fine thx right now i am not able to give you +50 because it's not completed 24 hours. – Pratik Apr 1 '16 at 13:25

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.