2

我想在我的自定义 html 块中重定向到特定的 url。

我正在尝试什么:

class Mage_Page_Block_Html_World extends Mage_Core_Block_Template {
function __construct()
{
    $i = 0;
    parent::__construct();
    $this->setTemplate('page/html/world.phtml');
    $this->setCookie();
}

public function setCookie()
{
    $lang = $this->getLanguageCode();
    if(isset($_GET['country'])) {
        $country = $_GET['country']; 
        Mage::getModel('core/cookie')->set('country', $country);
    } else {
        $country = Mage::getModel('core/cookie')->get('country');
    }
    /*Redirect to cookie url*/
    if($country) {
        try {
            $url = "http://myurl.dev/".$country."/";
            Mage::app()->getFrontController()->getResponse()->setRedirect($url);
        } catch (Exception $e) {
            echo 'Exception: ',  $e->getMessage(), "\n";
        }
    }
}

因为我在我的 .htaccess 中使用了多存储配置,所以我使用:

SetEnvIf Host www.myurl.dev MAGE_RUN_CODE=base
SetEnvIf Host www.myurl.dev MAGE_RUN_TYPE=website
SetEnvIf Host ^myurl.dev MAGE_RUN_CODE=base
SetEnvIf Host ^myurl.dev MAGE_RUN_TYPE=website

SetEnvIf 主机 www.myurl1.dev MAGE_RUN_CODE=vs
SetEnvIf 主机 www.myurl1.dev MAGE_RUN_TYPE=网站
SetEnvIf 主机 ^myurl1.dev MAGE_RUN_CODE=vs
SetEnvIf 主机 ^myurl1.dev MAGE_RUN_TYPE=网站

问题:通过此重定向,我收到错误 310:重定向过多。我不知道如何解决这个问题。

4

2 回答 2

4

考虑到框架架构,这是一种不合适的方法。作为开发人员,您可以使用多种机制来解析代码并触发重定向。当请求控制器层和事件观察器系统更合适时,您当前的方法让您使用视图层来触发重定向。

controller_action_predispatch事件在所有请求范围内都可用,并且每个路由都有针对性的预调度事件。您可以使用这些和Mage_Core_Controller_Request_Http对象(“请求对象”)来触发重定向。

于 2012-07-16T14:34:26.500 回答
1

太多的重定向意味着无限循环。听起来您正在每个页面上加载此块 - 这意味着即使用户被重定向,该块也会再次加载并仍然尝试重定向它们。

我建议您对国家/地区的检查需要更改:

if($country) {

一旦用户被重定向,此检查需要失败,以避免发送另一个重定向,目前看起来即使在重定向之后它也返回 true。

于 2012-07-16T11:02:10.090 回答