作为一个开发者的你,肯定要修改Magento代码去适应你的业务需求,但是在很多时候我们不希望修改Magento的核心代码,这 里有很多原因,例如将来还希望升级Magento、还想使用更多的Magento代码。如果你正在寻找修改Magento代码的最佳方式,那么此篇文章将 会是一个不错的教程。
适合对象:高级开发者
适合目标:开发者希望自定义修改Magento
当前版本:Magento versions: 1.4.0.1
作者:精东
最后修改时间:2010年7月13日
版本:V 0.3.0
重写Magento模块(Module)
第一步,你需要创建属于你自己代码的命名空间,例如Wemvc,App等,为了方便与大家分享代码,我将空间命名为App。
app/
code/
core/
community/
local/
App/
app/
code/
core/
community/
local/
App/
Catalog/
Block/
Breadcrumbs.php
etc/
config.xml
创建文件app/etc/modules/App_All.xml,添加如下代码。
< ?xml version="1.0"?>
<config>
<modules>
<App_Catalog>
<active>true</active>
<codePool>local</codePool>
</App_Catalog>
</modules>
</config>
重写Magento区块(Blocks)
编辑文件“app/code/local/App/Catalog/etc/config.xml”
<?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>
</global>
</config>
<blocks>
<catalog>
<rewrite>
<category_view>App_Catalog_Block_Category_View</category_view>
</rewrite>
</catalog>
</blocks>
在breadcrumbs标签中的值是你的类名,这样Magento就可以获取你的类,因为类名与你的目录名一致。用过zend framework的人都知道,自动加载auto loader这个东西,它会跟你类名中的下滑线去你的目录中需要对应的类文件。记住一点,下滑线代表下一级别的文件夹,如果你的类名与你的文件目录名不一 致,那么Magento根本不会理睬你。
举例来说:
App_Catalog_Block_Breadcrumbs → /app/code/local/App/Catalog/Block/Breadcrumbs.php App_Catalog_Block_Category_View → /app/code/local/App/Catalog/Block/Category/View.php
重写Magento控制器(Controller)-正则表达式匹配式
重写Magento控制器我们我们以重写购物车为例。
1、首先在App下创建新的模块,依次创建如下文件:
/app/code/local/App/Shopping /app/code/local/App/Shopping/etc /app/code/local/App/Shopping/etc/config.xml /app/code/local/App/Shopping/controllers /app/code/local/App/Shopping/controllers/CartController.php
<?xml version="1.0"?>
<config>
<modules>
<App_Shopping>
<version>0.1.0</version>
</App_Shopping>
</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[#^/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>
</global>
<!--
If you want to overload an admin-controller this tag should be <admin>
instead, or <adminhtml> if youre overloading such stuff (?)
-->
<frontend>
<routers>
<App_Shopping>
<!-- should be set to "admin" when overloading admin stuff (?) -->
<use>standard</use>
<args>
<module>App_Shopping</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>shopping</frontName>
</args>
</App_Shopping>
</routers>
</frontend>
</config>
< ?php
# 控制器不会自动加载,所以我们需要包含文件,这里与区块(Block)不一样
require_once 'Mage/Checkout/controllers/CartController.php';
class App_Shopping_CartController extends Mage_Checkout_CartController
{
#覆写indexAction方法
public function indexAction()
{
# Just to make sure
error_log('耶~成功重写购物车!');
parent::indexAction();
}
}
4、修改App_All.xml,激活我们新的Shopping模块
<?xml version="1.0"?>
<config>
<modules>
<App_Catalog>
<active>true</active>
<codePool>local</codePool>
</App_Catalog>
<App_Shopping>
<active>true</active>
<codePool>local</codePool>
</App_Shopping>
</modules>
</config>
5、修改视图文件app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml
在layout标签中,添加下面内容:
<app_shopping_cart_index>
<update handle="checkout_cart_index"/>
</app_shopping_cart_index>
到这里基本大功告成,但是,我建议你学习下正则表达式,因为刚刚的代码中,有这么一段:
<from>< ![CDATA[#^/checkout/cart/#]]></from>
还有一点,经过尝试,这里是可以支持同模块名覆盖的,例如Magento代码中商品详情页是Mage_Catalog_ProductController::viewAction(),如果我们想重写这个Controller,我们可以这样做:
a.简历新的目录/app/code/local/App/Catalog/controllers/ProductController.php
代码如下:
require_once 'Mage/Catalog/controllers/ProductController.php';/**
- Product controller
- @category Mage
- @package Mage_Catalog
/
class App_Catalog_ProductController extends Mage_Catalog_ProductController
{
/*- View product action
*/
public function viewAction()
{
echo ‘覆盖过的….’;
parent::viewAction();
}
}
- View product action
<?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>
<blocks>
<catalog>
<rewrite>
<breadcrumbs>App_Catalog_Block_Breadcrumbs</breadcrumbs>
</rewrite>
</catalog>
</blocks>
</global>
<frontend>
<routers>
<catalog>
<use>standard</use>
<args>
<module>App_Catalog</module>
<frontName>catalog</frontName>
</args>
</catalog>
</routers>
</frontend>
</config>
<?xml version="1.0"?>
<config>
<modules>
<App_Mycms>
<version>0.1.0</version>
</App_Mycms>
</modules>
<frontend>
<routers>
<mycms>
<use>standard</use>
<args>
<module>App_Mycms</module>
<frontName>mycms</frontName>
</args>
</mycms>
</routers>
</frontend>
<global>
<routers>
<cms>
<rewrite>
<index>
<to>App_Mycms/index</to>
<override_actions>true</override_actions>
<actions>
<noroute><to>App_Mycms/index/noroute</to></noroute>
</actions>
</index>
</rewrite>
</cms>
</routers>
</global>
</config>
重写Magento模型和动作助手(Model&Helper)
我们在改写Magento的过程中,为了实现自己的业务逻辑,难免要改它的业务模型。你可以尝试用模块下的配置文件配置你自己的类,继承你想重写的模型或者助手,然后调用自己的类。现在我们以用户模型为例深入讲解。
a.首先创建自己的模块文件夹
app/code/local/App/Customer app/code/local/App/Customer/etc/config.xml app/code/local/App/Customer/Model app/code/local/App/Customer/Model/Customer.php
<App_Customer>
<active>true</active>
<codePool>local</codePool>
</App_Customer>
<?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
}
}
<?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>
</models>
</global>
</config>
class App_Customer_Model_Address extends Mage_Customer_Model_Address {
// 重写已存在的方法
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
}
}
<?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
}
}
总结
在本章中我们学习了如何重写模块、重写控制器、重写区块,以及如何重写模型和助手,基本重写Magento代码对你来说已经不是难事了。文章至此, 要恭喜你,你已经掌握了大部分修改Magento的技能。下面的文章我们会进行更深入的研究。最后感谢所有Sasacake Team Member,是他们对待工作的热情和责任感促使我写这些教程。