3

I know of a couple of ways of redirecting to a given URL in observer but all of them uses exit or die and they would not work without it. So, if someone knows of a way of doing it without using exit or die then please let me know.

3

use setRedirect

<?php
namespace [Vendor]\[modulename]\Observer;
use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
class [YourClass] implements ObserverInterface {
    protected $_responseFactory;
    protected $_redirect;
    protected $_url;
    public function __construct(
        ......
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $redirect,
        ......
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_redirect = $redirect;

    }
    public function execute(Observer $observer) {
             $event = $observer->getEvent();
             $CustomRedirectionUrl = $this->_url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
             $this->_redirect->setRedirect($CustomRedirectionUrl);

    }
}
|improve this answer|||||
  • I am using above code for redirecting and its working when we exit; after sendResponse() but otherwise its not working. Also i should mention that my observer is observing layout_generate_blocks_after event . – Vivek Kumar Apr 5 '17 at 7:15
  • @Neo i update the code try it and feedback. – Qaisar Satti Apr 6 '17 at 9:29
  • The code you've updated works only for controllers and not for observers – Vivek Kumar Apr 6 '17 at 10:24
  • @Neo i am using this code in observer it is working for me. i am using this event controller_action_predispatch – Qaisar Satti Apr 6 '17 at 10:29
  • 1
    I've checked properly now and the code is working for me . Thanks – Vivek Kumar Apr 6 '17 at 11:27
1

Without exit or die redirecting to a given URL in observer you need to stop the dispatch event using Action::FLAG_NO_DISPATCH

namespace [Vendor]\[modulename]\Observer;

use Magento\Framework\Event\ObserverInterface;
class [YourClass] implements ObserverInterface {

    protected $urlManager;
    protected $actionFlag;
    protected $redirect;

    public function __construct(
        ......
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        ......
    ) {
        $this->urlManager = $url;
        $this->actionFlag = $actionFlag;
        $this->redirect = $redirect;

    }
    public function execute(\Magento\Framework\Event\Observer $observer) {
            $controller = $observer->getControllerAction();
            // stop the dispatch event. 
            $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
            //YOUR-ACTION eq to ex. for create acount '*/*/create'
            $defaultUrl = $this->urlManager->getUrl('YOUR-ACTION', ['_secure' => true]);
            $controller->getResponse()->setRedirect($this->redirect->error($defaultUrl));

    }
}
|improve this answer|||||
  • I used this code but I am getting following error : Uncaught Error: Call to a member function getResponse() – David Coder Dec 12 '18 at 6:06
  • I am using checkout_cart_product_add_after event – David Coder Dec 12 '18 at 6:07

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.