Magento: Redirect in Controller

Redirect is very popular in your programming life. After each successful action, we have to redirect customers to a success or thank you page. Sometimes, you also have to redirect customers to log in page. Today I am going to explain how to use redirect in controller of a Magento module and some tricks may be useful for your cases.

First of all, we can see two classes which we use the most frequently are: Mage_Core_Controller_Front_Action and Mage_Adminhtml_Controller_Action. Both of them extend abstract class Mage_Core_Controller_Varien_Action:

class Mage_Adminhtml_Controller_Action extends Mage_Core_Controller_Varien_Action
{
class Mage_Core_Controller_Front_Action extends Mage_Core_Controller_Varien_Action
{

Let's take a look at Mage_Core_Controller_Varien_Action class:

abstract class Mage_Core_Controller_Varien_Action
{
    /**
     * Request object
     *
     * @var Zend_Controller_Request_Abstract
     */
    protected $_request;

    /**
     * Response object
     *
     * @var Zend_Controller_Response_Abstract
     */
    protected $_response;

...

    /**
     * Retrieve request object
     *
     * @return Mage_Core_Controller_Request_Http
     */
    public function getRequest()
    {
        return $this->_request;
    }

    /**
     * Retrieve response object
     *
     * @return Mage_Core_Controller_Response_Http
     */
    public function getResponse()
    {
        return $this->_response;
    }
...

The normal scenario: When an HTTP request is sent to your server, it means a request object (Zend_Controller_Request_Abstract) that contains all HTTP request data is also created. After that, your code in controller use this data, models and other data (sessions, cookies, ...) to do some tasks. Finally, the controller returns a response object (HTTP response) to the client. So that, the redirect information that we are concerning should be in the response object. Luckily, it's TRUE :)

Take a look at the following function in this abstract class:

    /**
     * Set redirect url into response
     *
     * @param   string $url
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirectUrl($url)
    {
        $this->getResponse()->setRedirect($url);
        return $this;
    }

If you don't believe, let's take an exam for the following functions:

    /**
     * Set redirect into response with session id in URL if it is enabled.
     * It allows to distinguish primordial request from browser with cookies disabled.
     *
     * @param   string $path
     * @param   array $arguments
     * @return  Mage_Core_Controller_Varien_Action
     */
    public function setRedirectWithCookieCheck($path, array $arguments = array())
    {
...
    /**
     * Redirect to success page
     *
     * @param string $defaultUrl
     * @return Mage_Core_Controller_Varien_Action
     */
    protected function _redirectSuccess($defaultUrl)
    {
...
    /**
     * Redirect to error page
     *
     * @param string $defaultUrl
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirectError($defaultUrl)
    {
...
    /**
     * Set referer url for redirect in response
     *
     * @param   string $defaultUrl
     * @return  Mage_Core_Controller_Varien_Action
     */
    protected function _redirectReferer($defaultUrl=null)
    {
...
    /**
     * Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param)
     *
     * @return string
     */
    protected function _getRefererUrl()
    {
...

All of them are also useful functions for you to navigate in controller :)

However, in some case you need to navigate your site depending on some conditions. Should use return for this case to get true logic. For example:

 
class Yournamespace_Yourmodule_Yourcontroller extends Mage_Adminhtml_Controller_Action {
	public function testRedirect() {
		//...somecode here

	  if ($condition1) {
	  	//somecode here
	  	$this->_redirectUrl($url_1);
	  	return;
	  	if ($condtion2) {
	  		//somecode here
	  		$this->_redirectUrl($url_2);
	  		return;
	  	}
		} else if ($condition3) {
			//somecode here
	  	$this->_redirectUrl($url_3);
	  	return;
		}

		$this->_redirectUrl($url_n);
	}
}

If you don't use return in this example, your controller always redirect to $url_n according to the last function _redirect() in the action.

GLHF,
Thai Nguyen

Comments