以重写 app/code/core/Mage/Catalog/Block/Navigation.php 为例
1. 复制Mage (不推荐使用 )
First add a new Navigation.php file you can edit to your local folder that Magento will reference instead of the original.
Copy the original found here: app/code/core/Mage/Catalog/Block/Navigation.php
Recreate this folder structure in your app/code/local folder. So go to app/code/local, make a Mage folder, inside it make a Catalog folder, inside that make a Block folder, and inside that place your copy of Navigation.php, eg: app/code/local/Mage/Catalog/Block/Navigation.php
Second, add some code to the app/etc/local.xml file, inside of the global tags.
- <blocks>
- <catalog>
- <rewrite>
- <navigation>Mage_Catalog_Block_Navigation</navigation>
- </rewrite>
- </catalog>
- </blocks>
2. 重写 (推荐 )
If you want to override a core controller on Magento in order to add other customActions, it is really quick and easy to do so. Here are the files that you have to take into account:
- The block you want to override : /app/code/core/Mage/Catalog/Block/Navigation.php
- The xml file to enable the module: /app/etc/modules/[namespace] _All.xml
- The block xml file definition : /app/code/local/[namespace] /Catalog/etc/config.xml
- The overridden block : /app/code/local/[namespace] /Catalog/Block/Navigation.php
本文中所有 [namespace] 将被取代为我自己实例的名称 “App”,如果你想要用其他命名空间,可以替换[namespace]
First of all, you have to enable the module on the /app/etc/modules/ directory. This file would be App _All.xml and inside that:
- <?xml version="1.0"?>
- <config>
- <modules>
- <App_Catalog>
- <active>true</active>
- <codePool>local</codePool>
- <!--depends>
- <Mage_Core/>
- </depends-->
- </App_Catalog>
- </modules>
- </config>
开启depends的话,表示依靠core class,
Now it’s time to create the /app/code/local/App /Catalog/etc/config.xml file that will override the core block:
- <?xml version="1.0" encoding="UTF-8"?>
- <config>
- <modules>
- <App_Catalog>
- <version>0.1.0</version>
- </App_Catalog>
- </modules>
- <global>
- <blocks>
- <catalog>
- <rewrite>
- <navigation>App_Catalog_Block_Navigation</navigation>
- </rewrite>
- </catalog>
- </blocks>
- </global>
- </config>
注意:<navigation>App_Catalog_Block_Navigation</navigation> 中 App_Catalog_Block_Navigation 不能随便起名,一定要形成正确完整的路径, [namespace] _Catalog_Block_Navigation =>[namespace]/Catalog/Block/Navigation.php
Finally, we can create the class that will add/change actions to the core block:app/code/local/App/Catalog/Block/Navigation.php
- <?php
- class App_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation
- {
- protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
- $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false, $showText = false)
- {
- /* .... */
- }
- }
注意: class是继承了原来的Mage_Catalog_Block_Navigation
注意:如果要看效果,要清理cache先,或者在后台 disable cache