2

In Magento 1.x I m writing below code in Observer -

Mage::app()->getResponse()->setHttpResponseCode(403);
Mage::app()->getResponse()->clearBody();   
Mage::app()->getResponse()->sendHeaders();

Now I want to write same functionality in Magento-2 Observer. Please let know what Injunctions need to implement and how to write above code.

1 Answer 1

3

First you need to inject the following class in your observer constructor: \Magento\Framework\App\ResponseInterface for example:

protected $_response;

public function __construct(
    ...
    \Magento\Framework\App\ResponseInterface $response
) {
    ...
    $this->_response = $response;
}

Then in your code you can do:

$this->_response->setHttpResponseCode(403);
$this->_response->clearBody();
$this->_response->sendHeaders();
2
  • ahh.. you doing a great job
    – Amit Bera
    Commented Aug 31, 2016 at 13:31
  • Thx ...Raphael its works..... :)
    – Atul
    Commented Aug 31, 2016 at 13:36

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.