您的位置:首页 > 其它

magento如何重写控制器

2014-04-10 20:38 274 查看
首先建好如下目录先

app\code\local\Edcy\Shopping\Block

app\code\local\Edcy\Shopping\controllers

app\code\local\Edcy\Shopping\etc

app\code\local\Edcy\Shopping\Helper

app\code\local\Edcy\Shopping\Model

app\code\local\Edcy\Shopping\sql

这些都是常用到的目录,其中有些目录没有用到的,没有也没有关系。

新建如下文件,开启你的模块:

app\etc\modules\Edcy_Shopping.xml

内容如下:

<?xml version="1.0"?>

<config>

    <modules>

        <Edcy_Shopping>

            <active>true</active>

            <codePool>local</codePool>

        </Edcy_Shopping>

    </modules>

</config>

编写你的congfig配置文件:

app\code\local\Edcy\Shopping\etc\config.xml

文件里包含前端、后台、全局、模块版本这些东西。

<?xml version="1.0"?>
<config>
<modules>
<Edcy_Shopping>
<version>0.1.0</version>
</Edcy_Shopping>
</modules>
<global>
<rewrite>
<Edcy_Shopping_Product>
<from><![CDATA[#^/?catalog/product/#]]></from>
<to>/shopping/product/</to>
</Edcy_Shopping_Product>
</rewrite>
</global>
<frontend>
<routers>
<shopping>
<use>standard</use>
<args>
<module>Edcy_Shopping</module>
<frontName>shopping</frontName>
</args>
</shopping>
</routers>
<layout>
<updates>
<shopping>
<file>shopping.xml</file>
</shopping>
</updates>
</layout>
</frontend>
</config>上面from里的正则为什么需要这样写,我们之前也说过,网上教程大都是没有?的,这里我建议大家都加上问号,虽然一般情况下不会出错。

这样我们的配置文件就写好了,开始创建控制器
app\code\local\Edcy\Shopping\controllers\ProductController.php

require_once Mage::getModuleDir('controllers', 'Mage_Catalog') . DS . 'ProductController.php';
class Edcy_Shopping_ProductController extends Mage_Catalog_ProductController {
public function viewAction(){
// header("Content-Type: text/xml");
// die(Mage::app()->getConfig()->getNode()->asXML());
// exit;
echo '覆盖过的....';
parent::viewAction();
}

}上面呢,引入重写的控制器,我们可以直接用动态的路径,可以避免一些不必要的错误。
这样我们访问产品详细页就能看到我们输出的东西了。

下面还介绍一种重写方法

config.xml的配置内容如下:

<routers>
<!--这种写法将覆盖控制器所有的方法-->
<catalog>
<rewrite>
<product>
<to>shopping/product</to>
<override_actions>true</override_actions>
<actions>
<index>
<to>shopping/product/view</to>
</index>
</actions>
</product>
</rewrite>
</catalog>
</routers>

上面这段代码加到global节点下即可。
如果你的配置是这样写的话,那么你的 控制器里所有的方法都必须重写。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息