This has bothered me for a while and I've avoided it but the time has come! Often when extending core Magento functionality, whether using the Event-Observer pattern or by overriding a controller (only if absolutely necessary!), the core code will have already set a redirect using $this->getResponse()->setRedirect($url); or similar.

If I want to override that behaviour and have the response rendered in the current request/response cycle, how can I unset or nullify the redirect?

Looking through the type hierarchy to Zend_Controller_Response_Abstract which is the grandparent of Mage_Core_Controller_Response_Http, there are methods to clearHeaders() which would wipe out the Location header that is controlled by setRedirect() but obviously it will wipe out any other headers as well.

Worse case would be to getHeaders(), clearHeaders() and then iterate through all the other Headers with setHeader(), but that seems painful. Any suggestions welcome.

===============
EDIT

As pointed out by @xzyfer, the clearHeader() method does exist in some of the Magento editions. For clients still working on older codebases, how would you recommend implementing this. I'm hesitant to override Mage_Core_Controller_Response_Http as it's such a key class...

Thanks,
Jonathan

Comments

What version of magneto are you working on?

Written by xzyfer

duh. I'm normally the one bugging people to state what version :). I'm using Magento Community v1.4.2 for this project

Written by Jonathan Day

tbh there shouldn't be any reason why you couldn't replace the Zend_Controller_Response_Abstract from your system with the a newer one with the clearHeader() function. If you download 1.4.2 from the magento website it comes packaged with Zend 1.10.8 which has the `clearHeader() method.

Written by xzyfer

yup, I decided that was the best option too given that it's on the upgrade path anyway. Thanks for the advice.

Written by Jonathan Day

Accepted Answer

Zend_Controller_Response_Abstract has a clearHeader method

$this->getResponse()->clearHeader('Location');

You'll also have to override the response code:

$this->getResponse()->setHttpResponseCode(200);

Just a guess from a quick look at the source of Zend_Controller_Response_Abstract assuming no other magic is going on in one of the child classes this should do the job.

Written by xzyfer
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki