面向未来的Magento XML覆盖

时间:2011-05-13 13:36:50

标签: xml magento override

我知道Magento最佳实践模式,如果我想修改Magento的核心文件之一,我应该将文件(和匹配的目录树结构)从/ app / code / core复制到/ app / code / local并修改副本以使其覆盖原始副本。我需要使用Magento的核心XML文件之一 - 我修改了文件,我想确保在升级Magento后不会被覆盖。

现在,我有一个修改后的版本/app/code/core/Mage/Page/etc/config.xml - 我编辑它以添加我的网站所需的特定页面布局。现在我想将我的修改移出核心文件。我是否在/app/code/local/Mage/Page/etc/config.xml中重新创建整个XML文件,或者我只需要将我的添加和更改放入本地覆盖文件中吗?

如果是后者,有人可以向我解释这个结构吗?

2 个答案:

答案 0 :(得分:12)

你无法用这种方式“覆盖”一个config.xml文件。

Magento由代码模块组成。 Mage_Page是一个单独的模块。一个模块包含一个config.xml文件和许多PHP文件。

您可以在Magento中覆盖某些类型的 PHP 文件,方法是替换

app/code/local/Package/Module/Path/To/File.php

那是因为__autoloader创建了类似于

的include语句
include('Package/Path/To/File.php')

并且PHP将检查每个代码池中的文件。

app/code/local
app/code/community
app/code/core

通过在本地放置替换文件,Magento首先找到您的文件并跳过核心文件。

你不能对config.xml做同样的事情。如果您将config.xml文件放在

app/code/local/Mage/Page/etc/config.xml

Magento会忽略它。当您在Magento中请求页面时,系统将创建一个活动模块列表(来自app/modules/etc),然后加载每个模块的config.xml文件。您根本不应该更改核心config.xml文件。

根据您的问题,听起来您更改了此config.xml文件以添加其他layout.xml文件。如果是这种情况,那么你想要做的是

  1. 向magento添加新模块
  2. 在新模块的config.xml文件中,添加新的布局节点。
  3. This page(自我链接)涵盖了获取新模块所需的基本文件。但是,不是将观察者的内容添加到config.xml,而是添加新的布局节点

    <config>
        <frontend>
            <layout>
                <updates>
                    <my_unique_node_name>
                        <file>foo.xml</file>
                    </my_unique_node_name>
                </updates>
            </layout>
        </frontend>
    </config>    
    

答案 1 :(得分:2)

如果它不是太多的xml,你可以将它弹出到app / etc / local.xml,这是我倾向于自定义布局。

E.g。

<config>
  <global>
    <!-- ... -->
    <page>
      <layouts>
        <three_columns_test module="page" translate="label">
          <label>3 Columns Test</label>
          <template>page/3columns-test.phtml</template>
          <layout_handle>page_three_columns_test</layout_handle>
        </three_columns_test>
        <new_homepage module="page" translate="label">
          <label>Homepage Layout</label>
          <template>page/homepage.phtml</template>
          <layout_handle>page_homepage</layout_handle>
        </new_homepage>
      </layouts>
    </page>
    <!-- ... -->
  </global>
</config>