Magento:使用控制器\u操作\u后分派事件观察器将块前置到Mage头块

Magento:使用控制器\u操作\u后分派事件观察器将块前置到Mage头块,magento,Magento,我试图使用自定义事件观察者来观察控制器\u动作\u后调度事件,并通过将开头标记替换为+我的自定义块内容来直接修改响应正文。重要的是,我要确保它是一个实际的HTML页面响应,而不是JSON或AJAX请求中的部分HTML。我想使用controller\u action\u layout\u render\u before事件来确定头块是否存在,设置一个标志,前面提到的观察者方法将在修改响应之前检查该标志 用例是在每个页面的head标记中安全地预先添加我的自定义块,优先于其他head内容,如meta标

我试图使用自定义事件观察者来观察
控制器\u动作\u后调度
事件,并通过将开头标记替换为+我的自定义块内容来直接修改响应正文。重要的是,我要确保它是一个实际的HTML页面响应,而不是JSON或AJAX请求中的部分HTML。我想使用
controller\u action\u layout\u render\u before
事件来确定头块是否存在,设置一个标志,前面提到的观察者方法将在修改响应之前检查该标志

用例是在每个页面的head标记中安全地预先添加我的自定义块,优先于其他head内容,如meta标记和脚本标记

这就是我现在所处的位置(不远,有点迷路了)

config.xml
我可能会用比拦截输出更标准的方法来解决这个问题。您可以通过在布局中附加子块并在主题中使用自定义模板文件输出它来完成所需的操作

$theme/layout/local.xml

<default>
    <reference name="head">
        <!-- If you need a custom block, just swap in the type -->
        <block type="core/template" name="prepend-head" template="page/html/prepend-head.phtml"/>
    </reference>
</default>

$theme/template/page/html/head.phtml

<default>
    <reference name="head">
        <!-- If you need a custom block, just swap in the type -->
        <block type="core/template" name="prepend-head" template="page/html/prepend-head.phtml"/>
    </reference>
</default>

在文件顶部附近的某个地方添加

有什么问题吗?看起来就像observer所需要的一切,虽然我会将events标记放在前端,但我不知道如何成功且安全地将我的块预先添加到head块。我还质疑我在“checkResponse()”方法中的检查是否正确。$observer->loadLayout()->getBlock('head')->addBlock('yourBlock'),检查页面xml以查看head等的适当名称。重要的是,我>将<我的块前置到head块,而不是追加它。另外,当我在我的observer上调用loadLayout()时,我的店面似乎就爆炸了。哦,那就把它删除吧,$observer->loadLayout()->removeBlock('head')->addBlock('yourBlock')->addBlock('head'),但不知怎的,我觉得prepend这个词不合适。。。
<default>
    <reference name="head">
        <!-- If you need a custom block, just swap in the type -->
        <block type="core/template" name="prepend-head" template="page/html/prepend-head.phtml"/>
    </reference>
</default>