Magento 重写总结 (一)

piaoling  2011-07-13 16:16:34

记得有篇关于《Magento开发者最容易出现的3个错误》文章里面提到的一点就是——-修改 Magento 的核心文件, 这是很多新的magento开发者最容易犯的一个毛病, 一旦修改了magento的核心文件,以后如果要对magento进行升级, 那将是一个恶梦, 同时,也容易造成不同模块间的冲突, 背离magento模块之间低耦合的设计思想。

今天, 我们一起来回顾一下,如何对magento的model, block, controller 进行重写

首先, 我们要创建一个新的module, 具体方法可以参见这里

一个新的module是一个独立的模块, 还是重写magento核心的模块的关键在于, 配置config.xml文件, 那么如何通过配置config.xml来重写magento的model, block 和controller呢?

1. 针对block 和 model

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <App_Catalog>
            <version>0.1.0</version>
        </App_Catalog>
    </modules>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                        <breadcrumbs>App_Catalog_Block_Breadcrumbs</breadcrumbs>
                </rewrite>
            </catalog>
        </blocks>
 
        <models>
            <customer>
                <rewrite>
                    <customer>App_Customer_Model_Customer</customer>
                </rewrite>
            </customer>
        </models>
 
    </global>
</config>

2. 针对contoller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <App_Catalog>
            <version>0.1.0</version>
        </App_Catalog>
    </modules>
    <global>
        <!-- This rewrite rule could be added to the database instead -->
        <rewrite>
            <!-- This is an identifier for your rewrite that should be unique -->
            <!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
            <App_Shopping_cart>
                <from><![CDATA[#^/catalog/product/#]]></from>
                <!--
                    - Shopping module matches the router frontname below - checkout_cart
                    matches the path to your controller Considering the router below,
                    "/shopping/cart/" will be "translated" to
                    "/App/Shopping/controllers/CartController.php" (?)
                -->
                <to>/catalog/product/</to>
            </App_Shopping_cart>
        </rewrite>
    </global>
    <frontend>
        <routers>
            <catalog>
                <use>standard</use>
                <args>
                    <module>App_Catalog</module>
                    <frontName>catalog</frontName>
                </args>
            </catalog>
        </routers>
    </frontend>
</config>

有一点需要注意的是,你需要把这个模块的所有Controller都重写,这可能会带来很大的麻烦, 怎么可以避免这个问题呢, 情况下面的config.xml写法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <App_Catalog>
            <version>0.1.0</version>
        </App_Catalog>
    </modules>
 
    <frontend>
        <routers>
            <catalog>
                <use>standard</use>
                <args>
                    <module>App_Catalog</module>
                    <frontName>catalog</frontName>
                </args>
            </catalog>
        </routers>
    </frontend>
 
    <global>
        <routers>
            <catalog>
                <rewrite>
                        <to>App_Catalog/catalog</to>
                        <override_actions>true</override_actions>
                        <actions>
                           <noroute><to>App_Catalog/catalog/product</to></noroute>
                        </actions>
                </rewrite>
            </catalog>
        </routers>
    </global>
 
</config>

以上都是一些常见的重写方式, 以后我们还会添加一些特别的重写方法.

类别 :  magento(258)  |  浏览(3705)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: