Future-proofing a Magento XML override

China☆狼群 提交于 2019-12-19 11:22:32

问题


I'm aware of the Magento best-practices pattern that says that if I want to modify one of Magento's core files, I should copy the file (and matching directory tree structure) from /app/code/core into /app/code/local and modify the copy so that it overrides the original. I need to do that with one of Magento's core XML files - I've modified the file, and I want to make sure that it doesn't get overwritten later when I upgrade Magento.

Right now, I have a modified version /app/code/core/Mage/Page/etc/config.xml in place - I edited it to add in a specific page layout that I need for my site. Now I want to move my modification out of the core files. Do I recreate the entire XML file in /app/code/local/Mage/Page/etc/config.xml, or do I only need to put my additions and changes into a local override file?

If it's the latter, can someone explain the structure of this to me please?


回答1:


If it's not too much xml, you can pop it into app/etc/local.xml which is what I tend to do for custom layouts.

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>



回答2:


You can't exactly "override" a config.xml file that way.

Magento is made up of code modules. Mage_Page is a single module. A module contains a config.xml file, and numerous PHP files.

You can override certain types of PHP files in Magento by placing a replacement in

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

That's because the __autoloader creates include statements that look like this

include('Package/Path/To/File.php')

and PHP will check each code pool for a file.

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

By placing a replacement file in local, Magento finds your file first and skips the core file.

You can't do the same thing with config.xml. If you place a config.xml file at

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

Magento will ignore it. When you request a page in Magento, the system will create a list of active modules (from app/modules/etc), and then load each module's config.xml file. You shouldn't be changing core config.xml files at all.

Based on your question, it sounds like you changed this config.xml file to add an additional layout.xml file. If that's the case, then what you want to do is

  1. Add a new module to magento.
  2. In your new module's config.xml file, add the new layout node.

This page (self link) covers the basic files you need to get a new Module in place. However, instead of adding the observer stuff to config.xml, add your new layout node instead

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


来源:https://stackoverflow.com/questions/5992714/future-proofing-a-magento-xml-override

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!