重寫某個magento模組的某個controller

來源:互聯網
上載者:User

 

重寫某個magento
模組的某個controller

當你需要對magento
的某個功能進行修改時,建立新的模組重寫現有模組的某個controller
是必然的,因為你只是想修改其中的某個方法,而不需要動其它的controller
,其它的類。

 

 

 

以重寫checkout

模組的 OnepageController.php

為例子:

 

 

 

step
one:
建立新並註冊新的模組

 

這個應該不用多說了,可以直接複製一個現有模組的檔案目錄結構,也可以只建立你需要修改的檔案,當然要保持其目錄結構層次。而且要包含必須的檔案:每個模組的必須檔案是config.xml
,然後則是你要修改的那個檔案。

 

模組檔案:

 

Magento/app/code/local/Company/Xcheckout/etc/config.xml

Magento/app/code/local/Company/Xcheckout/controllers/OnepageController.php

註:我想理論上講我們也可以在新的Company

包(package
)中使用checkout
作為模組名,但是我在實現過程中就因為這個出現問題,而且儘管我嘗試跟蹤代碼想找出原因,卻發現好像magento
本身就是那麼處理的,如果我判斷沒錯的話,也許是一個不算bug
的bug
吧。

 

向magento
註冊新模組檔案:

 

Magento/app/etc/modules/Company_All.xml

 

<config>

<modules>

<Company_Xcheckout>

<active>true</active>

<codePool>core</codePool>

<depends>

<Mage_Checkout
/>

</depends>

</Company_Xcheckout>

</modules>

</config>

 

註:雖然說沒指定depends
也是可以的,不過我覺得顯然我們的模組是要在原有的checkout
模組基礎上建立的,所以事實上是有依賴的,增加這個是比較合理的。

 

 

 

step2:
編輯建立模組的config.xml
檔案,配置你要重寫的controller

 

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

<config>

<modules>

<Company_Xcheckout>

<version>0.1.0</version>

</Company_Xcheckout>

</modules>

<global>

<rewrite>

//
原本以為這個名稱有分大小寫,而事實上是沒有的。由於magento
沒有一個參考的文檔,我們只能通過代碼來驗證。但是我在跟蹤發現了這個並不現在magento
論壇上他們所講的那樣是有分大小寫,當然,它必須是唯一的。

<Company_xcheckout_onepage>

<from><![CDATA[#^/checkout/onepage/#]]></from>

<to>/xcheckout/onepage/</to>

</Company_xcheckout_onepage>

</rewrite>

 

//
重寫magento
block

<blocks>

<checkout>

<rewrite>

<onepage>Company_Xcheckout_Block_Onepage</onepage>

<onepage_billing>Company_Checkout_Block_Onepage_Billing</onepage_billing>

</rewrite>

</checkout>

</blocks>

</global>

 

//
如果你發現你的controlller
沒有被調用,基本上是router
設定不合理。

<frontend>

<routers>

<Company_Xcheckout>

<use>standard</use>

<args>

<module>Company_Xcheckout</module>

<frontName>xcheckout</frontName>

</args>

</Company_Xcheckout>

</routers>

 

//
如果你在實施的時候出現空白頁之類的問題,基本上是由於layout
引起的

<layout>

<updates>

<checkout>

<file>xcheckout.xml</file>

</checkout>

</updates>

</layout>

</frontend>

</config>

 

 

 

step
3:
重寫相關的代碼

 

1
、重寫controller

 

require_once
"Mage/Checkout/controllers/OnepageController.php";

 

class
Company_Xcheckout_OnepageController extends
Mage_Checkout_OnepageController

{

//do
what you want

 

//
盡情發揮吧

 

}

 

2
、重寫block

 

require_once
'Mage/core/Checkout/Block/Onepage.php';

 

class
Company_Xcheckout_Block_Onepage extends
Mage_Checkout_Block_Onepage_Abstract

{

 

}

 

註:其實在實現和跟蹤代碼的過程中還發現了很多問題和學到了magento
的一些在文檔上沒有東西。只能等有時間一點點整理了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.