I have this code to redirect to custom page from observer.
But I wonder how to redirect from observer, and with data transfer to the redirected page.
<?php
namespace Namespace\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\RedirectInterface;
class MyObserver implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
// do something
// redirect
$this->redirectToMyCustomPage($observer);
return $this;
}
public function redirectToMyCustomPage($observer) {
// get error url
$url = '/namespace/module/action';
// setting an action flag to stop processing further hierarchy
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
/// redirecting to error page
$observer->getControllerAction()->getResponse()->setRedirect($url);
return $this;
}
}