重定向到最后一页的可靠方法


8

magento是否有可靠的方法重定向到最后一页。

我有一个小表格,如果发生错误,我想重定向到上一页。

该表单位于产品页面上,成功完成后将重定向到另一个页面。但是,如果发生错误,我想重定向回产品页面。

我的动作网址是 /my/module/submit

另外,当我说可靠的时候,我的意思是redirectReferer不能依靠


1
RedirectReferer足够可靠,Magento的Adminhtml可以完全依靠后退按钮吗?
philwinkle

Answers:


23

Magento依赖于$_SERVER['HTTP_REFERER']重定向逻辑的值而没有任何限制;见Mage_Core_Controller_Varien_Action

/**
 * Set referer url for redirect in responce
 *
 * @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;
}

/**
 * Identify referer url via all accepted methods (HTTP_REFERER, regular or base64-encoded request param)
 *
 * @return string
 */
protected function _getRefererUrl()
{
    $refererUrl = $this->getRequest()->getServer('HTTP_REFERER');
    if ($url = $this->getRequest()->getParam(self::PARAM_NAME_REFERER_URL)) {
        $refererUrl = $url;
    }
    if ($url = $this->getRequest()->getParam(self::PARAM_NAME_BASE64_URL)) {
        $refererUrl = Mage::helper('core')->urlDecode($url);
    }
    if ($url = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) {
        $refererUrl = Mage::helper('core')->urlDecode($url);
    }

    if (!$this->_isUrlInternal($refererUrl)) {
        $refererUrl = Mage::app()->getStore()->getBaseUrl();
    }
    return $refererUrl;
}

如果您不满足于依赖$_SERVER['HTTP_REFERER'],那么您唯一可以做的就是将最新访问的URL存储在session中,为此值创建一个getter来处理有人直接导航到该站点的情况

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.