`
coolsooner
  • 浏览: 1311475 次
文章分类
社区版块
存档分类
最新评论

magento 模块重写机制

 
阅读更多

由magento重写功能所想到的:

1.在etc/config.xml中

第一层为<config>

第二层为<module>,<global>,<admin>,<adminhtml>,<frontend>,<default>等。

2.重写内容

1>block--在config.xml文件中block重写的配置

<global>

<blocks>

<模块名>

<rewrite>

<重写的半类名>类名全名

如:

<global>

<blocks>

<catelog>

<rewrite>

<breadcrumbs>App_Catalog_Block_Breadcrumbs</breadcrumbs>

2>controller---在config.xml文件中的配置

<global>

<rewrite>

<你的控制器的名>

<from><![CDATA[#^/模块名/controller/#]]> </from>

<to>/shopping/cart/</to>

例如:

<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[#^/checkout/cart/#]]></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>/shopping/cart/</to>

</App_Shopping_cart>

</rewrite>

<!--参考-->

<blocks>

<catalog><class>Mage_Catalog_Block</class></catalog>

</blocks>

</global>

3> controll文件的修改:

首先

# 控制器不会自动加载,所以我们需要包含文件,这里与区块(Block)不一样

然后extends这个包含的文件。

重写方法。

4>修改视图文件:

<app_shopping_cart_index>

<update handle="checkout_cart_index"/>

</app_shopping_cart_index>

访问shopping/cart/index==checkout/cart/index!

5>重写Magento模型

<?xml version="1.0" encoding="UTF-8"?>

<config>

<modules>

<App_Customer>

<version>0.1.0</version>

</App_Customer>

</modules>

<global>

<models>

<customer>

<rewrite>

<customer>App_Customer_Model_Customer</customer>

</rewrite>

</customer>

</models>

</global>

</config>

class App_Customer_Model_Customer extends Mage_Customer_Model_Customer {

// 重写已存在的方法

public function validate() {

// Define new validate rules. From now magento call this validate method instead of existing method

//return $errors;

return true;

}

// 你还可以创建新的方法

public function newMethod() {

// function logic

}

}

6>重写Magento的模型资源

<?xml version="1.0" encoding="UTF-8"?>

<config>

<modules>

<App_Customer>

<version>0.1.0</version>

</App_Customer>

</modules>

<global>

<models>

<customer>

<rewrite>

<customer>App_Customer_Model_Customer</customer>

<address>App_Customer_Model_Address</address>

</rewrite>

</customer>

<customer_entity>

<rewrite>

<address>App_Customer_Model_Entity_Address</address>

</rewrite>

</customer_entity>

</models>

</global>

</config>

class App_Customer_Model_Entity_Address extends Mage_Customer_Model_Entity_Address {

protected function _afterSave(Varien_Object $address) {

// Write your code

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics