Magento2 redirection from Observer

Magento2 redirection from Observer

Magento2 redirection from Observer needs serval times.In Some events, you need to forcefully redirect to some pages basic your business logic.

At magento 1.x,we can do using Mage::app()->getResponse()->setRedirect(‘YourRedirectUrl’)->sendResponse();

 

Something is required forMagent0 2.X version.This version uses modern technologies   Namespace, interface, Factory class etc.So there not easy to do a redirection from an Observer.

Most case,  developer need to implement forceful redirection  to Observer as per their client requirement

===========================================

 

I have found a solution by myself by doing a research

If you want to do that then you should below inject two classes.

  • First, \Magento\Framework\App\ResponseFactory which responsible for redirection,
  • Another class which\Magento\Framework\UrlInterface will make an url for that redirection.(This can optional).
  • Then You should create  a response Object  $this->_response->create()  from Magento\Framework\App\ResponseFactory.
  • Then using method setRedirect() and sendResponse(),you can redirect url.Where setRedirect() set url for redirection and sendReponse() send response this url.

 

 

Observer

 

Case1: For Controllers events

  • ‘controller_action_predispatch’
  • ‘controller_action_predispatch_’ . $request->getFullActionName() . like controller_action_predispatch_customer_account_logout
  •  ‘controller_action_predispatch_’ . $request->getRouteName().  like controller_action_predispatch_customer
  • ‘controller_action_postdispatch
  • ‘controller_action_postdispatch_’ . $request->getFullActionName()  . like controller_action_postdispatch_customer_account_logout
  •  ‘controller_action_postdispatch_’ . $request->getRouteName().
  •   like controller_action_postdispatch_customer

 

If  want redirection from Observer for an event which has controller_action object as it  events parameters

Then time, my suggestion avoid using \Magento\Framework\App\ResponseFactory  because of

If you have controller_action object and by injecting \Magento\Framework\App\ActionFlag   then  using below code you redirect to your desired URL

 

 

Example:

Here i am written an example of that redirection.

I have used event,checkout_cart_add_product_complete I was trying to forcefully redirect to Customer login page

Here the observer code:

Controller event example:

For this case ,I have used event controller_action_postdispatch_newsletter_subscriber_newI was trying to forcefully redirect to Customer login page


 

4 thoughts on “Magento2 redirection from Observer

  • You could do this in your execute method instead:

    $this->_actionFlag->set(”, Action::FLAG_NO_DISPATCH, true);
    $this->redirect->redirect($controller->getResponse(), ‘redirect/path/here’);

    • correction

      $controller = $observer->getControllerAction();
      $this->_actionFlag->set(”, Action::FLAG_NO_DISPATCH, true);
      $this->redirect->redirect($controller->getResponse(), ‘catalog/product/view/id/1’);

      • Aaron, thanks for this info.

        Your codes do not all work for all events bcoz of for every event you cannot get controller action.

        but You can get \Magento\Framework\App\ResponseFactory $responseFactory at everywhere in frontend that why i have use

Leave a Reply

Your email address will not be published. Required fields are marked *