Skip to main content

How to redirect a customer to another URL from Observer

In order to redirect a customer from observer you need to use another way than the Controller 

 

protected $_response;
protected $_objectManager;
public function __construct(
    \Magento\Framework\App\ResponseInterface $response,
    \Magento\Framework\ObjectManagerInterface $objectManager,
    ) {
        $this->_response = $response;
       $this->_objectManager = $objectManager;
   }

 

then in your function you can use this 

 

$urlInterface = $this->_objectManager->get('Magento\Framework\UrlInterface');
// URL to redirect to
$url = $urlInterface->getUrl('customer/account/index');
$this->_response->setRedirect($url)->sendResponse();