使用url helper更新Magento layout.xml - 我总是得到一个数组而不是一个url

时间:2013-08-21 23:16:45

标签: magento

我想在一些将由扩展程序生成的网站中插入规范元标记。所以我在扩展名的layout.xml中插入了以下代码:

    <reference name="head">
        <action method="addLinkRel">
            <rel>canonical</rel>
            <href><url helper="core/url/getCurrentUrl"/></href>
        </action>
    </reference>

但我总是得到“数组”而不是网址。我做错了什么?

如果我能让它发挥作用,我是否只使用www.mystore.com/productxy.html得到www.mystore.com/productxy.html?page=3或完整的网址。 因为我只需要第一个,没有参数。

2 个答案:

答案 0 :(得分:1)

您的代码几乎是正确的。虽然您只能在helper标记下的标记上使用布局xml中的<action>属性。幸运的是,您错误地添加了额外的<url>标记,因此这应该有效:

<reference name="head">
    <action method="addLinkRel">
        <rel>canonical</rel>
        <href helper="core/url/getCurrentUrl"/>
    </action>
</reference>

Mage_Core_Helper_Url::getCurrentUrl()会从您的REQUEST_URI变量中返回$_SERVER。该变量包含查询,因此不幸的是它并不像你想象的那样可用。

答案 1 :(得分:0)

我很确定你做不到。您收到Array因为它将<url helper="core/url/getCurrentUrl"/> XML节点解释为Array。此操作处理函数addLinkRel而不是<url />帮助程序(从不)。

更好(也更有趣)的方法是创建一个模块,您可以在其中定义呈现<link rel='canonical' href='{$currentUrl}' />的新块类型。

这是我将如何做到这一点,它将需要大约4个文件:

应用程序/代码/小区/ Electricjesus /典型的/ etc / config.xml中

<?xml version="1.0"?>
<config>
  <modules>
    <Electricjesus_Canonical>
      <version>0.1.0</version>
    </Electricjesus_Canonical>
  </modules>
  <global>
    <blocks>
      <canonical>
          <class>Electricjesus_Canonical_Block</class>
      </canonical>
    </blocks>    
  </global>
</config> 

应用程序/代码/小区/ Electricjesus /规范/砌块/ Link.php

<?php
class Electricjesus_Canonical_Block_Link extends Mage_Core_Block_Template {

}

应用程序/设计/前端/碱/默认/模板/规范/ link.phtml

<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl(); ?>
<link rel="canonical" href="<?php echo $currentUrl ?>" />

应用程序的/ etc /模块/ Electricjesus_Canonical.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Electricjesus_Canonical>
      <active>true</active>
      <codePool>community</codePool>
      <version>0.1.0</version>
    </Electricjesus_Canonical>
  </modules>
</config>

所以,现在这样做的方法是(在local.xml中):

<reference name="head">
    <block type="canonical/link" name="canonical_link" template="canonical/link.phtml" />
</reference>

所以这基本上只是我在几分钟内制作的粗略草稿,我已经使用相同的解决方案来解决另一类问题(但范围相似)。所以,如果你愿意,可以给它一个旋转。