首先建好如下目錄先
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節點下即可。
如果你的配置是這樣寫的話,那麼你的 控制器裏所有的方法都必須重寫。