Magento - redirecting a client from an observer method

In this case, checkout_cart_add_product_complete , I want the client to be redirected to an external web page http://www.example.com/ . For this, I use this code, which does not work at all: -

 public function moduleMethod() { /* @var $response1 Mage_Core_Controller_Response_Http */ $response1 = $observer->getEvent()->getResponse(); /* @var $response2 Mage_Core_Controller_Response_Http */ $response2 = Mage::app()->getResponse(); $url = 'http://www.example.com/'; $response1->setRedirect($url); return; } 

I used the " setRedirect() " method for both of these variables " $response1 " and " $response2 ", but both of them show me the Cart page, whereas I want to see this page http://www.example.com/ instead of this.

What I want:

  • I donโ€™t want to redefine the controller class to redirect the client when I can use the event observation process efficiently.
  • I donโ€™t want to use the built-in PHP function " header() " when the Magento framework effectively provides this functionality.
+8
redirect php magento
source share
3 answers

tl; dr: The correct observer code below.

Note before answering: make sure that the observer is activated; execute your code or use die('here'); . As written, your sample method does not have the correct prototype for receiving observer data for events (there is no argument).

Using an event observer for the forwarding logic in this context is quite appropriate, since the main command explicitly passes the request and response objects to the observer. Your attempt is good, but I think that you have conditions and settings that cause the thread to Mage_Checkout_CartController::_goBack() before Mage_Checkout_CartController::_goBack() , in particular, to the line

 $this->_redirect('checkout/cart'); 

Therefore, we need to rethink our approach. Now you can prevent the processing of any request / response logic after your event observer by manipulating the response and calling the Front Controller sendResponse() method, as shown below ( nb: do not do this! ):

 public function moduleMethod($observer) //note I added a param { /* @var $response1 Mage_Core_Controller_Response_Http */ $response1 = $observer->getResponse(); // observers have event args $url = 'http://www.example.com/'; $response1->setRedirect($url); /* SHOULDN'T DO THIS */ Mage::app()->getFrontController()->sendResponse(); } 

This should work, but I think it mixes the areas of concern, causing the system to exit the ether component of the system (EDA). Let's see if there is something in the command management structure that we can use ...

Immediately after the checkout_cart_add_product_complete event is executed, the _goBack () cart controller is called. The problem with this method name is that it does more than its name:

 /** * Set back redirect url to response * * @return Mage_Checkout_CartController */ protected function _goBack() { $returnUrl = $this->getRequest()->getParam('return_url'); if ($returnUrl) { // clear layout messages in case of external url redirect if ($this->_isUrlInternal($returnUrl)) { $this->_getSession()->getMessages(true); } $this->getResponse()->setRedirect($returnUrl); } //... } 

It looks like we can just set the return_url parameter to the request object and do what we need.

 public function moduleMethod(Varien_Event_Observer $observer) { $observer->getRequest()->setParam('return_url','http://www.google.com/'); } 

I tested this and it should do the trick!

+16
source share

I had to go through getFront :

 public function moduleMethod($observer) { $observer->getEvent()->getFront()->getResponse()->setRedirect('http://www.google.com'); } 
+2
source share

You can use the _ redirectUrl () method, it is used to redirect to external websites from magento.

+1
source share

All Articles