html - Magento 从 HTML block 重定向

标签 html magento redirect block

我想将我的自定义 html block 重定向到特定的 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 主机 www.myurl.dev MAGE_RUN_CODE=base
SetEnvIf 主机 www.myurl.dev MAGE_RUN_TYPE=网站
SetEnvIf 主机 ^myurl.dev MAGE_RUN_CODE=base
SetEnvIf 主机 ^myurl.dev MAGE_RUN_TYPE=网站

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:重定向过多。 我不知道如何解决这个问题。

最佳答案

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

controller_action_predispatch 事件适用于所有请求范围,并且每个路由都有目标预分派(dispatch)事件。您可以使用它们和 Mage_Core_Controller_Request_Http 对象(“请求对象”)来触发重定向。

关于html - Magento 从 HTML block 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11501091/

相关文章:

mysql - Magento 重建索引问题 - 一般错误 : 1005 Can't create table (errno: 121)

amazon-web-services - S3 和 CloudFront 网站托管用于多个 SPA 根据 url 路径重定向到不同的 index.html

javascript - express 中的 res.redirect 返回 html 源代码而不仅仅是 URL

wpf - 如何在MVVM-WPF中的某个事件上激活新页面?

javascript - 使用 jquery 创建动态 html 页面

soap - 通过 SOAP 调用获取图像缩略图

Magento 集合迭代器 - 无法获取附加属性

html - 如何让两个子div的高度彼此相同

html - 如何从我的页面中删除一大片空白区域?

html - 创建这种类型的网格