2

How can I detect the URL and store it then use it on the Controller for Redirect. Can anyone help me with this?

2 Answers 2

10

To get current URL in Magento use below code.

$currentUrl = Mage::helper('core/url')->getCurrentUrl()

But i think your requirement is to get the previous URL & redirect from controller. If it so use below code.

$this->_redirectReferer()
1
  • Thanks it is correct to use the $this->_redirectReferer() on the controller.
    – Alen
    Commented Dec 1, 2015 at 1:28
1

In controller you can using redirect when using one of the following functions:

/**
 * Set redirect into responce
 *
 * @param   string $path
 * @param   array $arguments
 */
protected function _redirect($path, $arguments=array())
{

}

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

/**
 * Set referer url for redirect in response
 *
 * @param   string $defaultUrl
 * @return  Mage_Core_Controller_Varien_Action
 */
protected function _redirectReferer($defaultUrl=null)
{

    $refererUrl = $this->_getRefererUrl();
    if (empty($refererUrl)) {
        $refererUrl = empty($defaultUrl) ? Mage::getBaseUrl() : $defaultUrl;
    }

    $this->getResponse()->setRedirect($refererUrl);
    return $this;
}

/**
 * Generate url by route and parameters in Adminhtml
 *
 * @param   string $route
 * @param   array $params
 * @return  string
 */
public function getUrl($route='', $params=array())
{
    return Mage::helper('adminhtml')->getUrl($route, $params);
}

Get URL of a path: Mage::getUrl($path, $arguments);

Example

You want redirect to controller of path: sales/order/view with params order_id=8

$this->_redirect('sales/order/view', array('order_id' => 8));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.