2012-02-08 52 views
2

我已經對Mage_Core_Controller_Request_Http進行了一些更改,但在由magento發佈的文件中。我知道這不是最好的方式,但我無法解決如何覆蓋Controller目錄中的文件。我可以找出如何覆蓋控制器目錄中的文件。我如何覆蓋Mage_Core_Controller_Request_Http

有誰能告訴我如何在我自己的擴展中覆蓋Mage_Core_Controller_Request_Http。

感謝

+0

您是否嘗試過創建觀察者,以傾聽您可以輕鬆覆蓋的某些預調度事件?爲什麼要覆蓋/擴展整個「Mage_Core_Controller_Request_Http」類? – ShaunOReilly 2012-02-08 23:20:40

回答

9

如果您不想恢復到包含路徑破解,您還可以使用反射在Mage_Core_Model_App模型上設置您自己的請求類。你可以使用一個觀察者爲controller_front_init_before事件。
我假設你很熟悉如何創建一個事件觀察者,所以我只能添加觀察者方法的代碼。如果您需要更多信息,請詢問。

// Observer method 
public function controllerFrontInitBefore(Varien_Event_Observer $observer) 
{ 
    $app = Mage::app(); 
    $reflection = new ReflectionClass($app); 
    $property = $reflection->getProperty('_request'); 
    $property->setAccessible(true); 
    $myRequest = new Your_Module_Controller_Request_Http(); 
    $myRequest->setOrigRequest($app->getRequest()); // if needed 
    $property->setValue($app, $myRequest); 

    // Proof of concept: 
    // Loggs Your_Module_Controller_Request_Http 
    Mage::log(get_class(Mage::app()->getRequest())); 
} 

創建類Your_Module_Controller_Request_Http和延長原來Mage_Core_Controller_Request_Http
事件發生後,將使用您的請求對象而不是原始對象。

這使您可以保持儘可能安全的升級,因爲您不必從cor code pool中複製完整的課程。

+0

感謝Vinai。這讓我頭腦發熱:) – 2012-02-09 11:54:50

+0

企業版用戶警告:這會破壞FPC,因爲處理器在_controller_front_init_before_事件之前將請求重新路由到_pagecache/request/process_,所以當替換請求對象時將需要的信息複製到新的信息。 – rcason 2015-07-31 11:14:13

5

編輯:威奈的解決方案是最好的一個。

因爲這個類是直接實例化的,所以必須使用包含路徑hack的所謂來覆蓋。

影響Varien_Autoload工作的包含路徑的優先順序在app/Mage.php中設置。這個順序如下:

  1. app/code/local/
  2. app/code/community/
  3. app/code/core/
  4. lib/

因此,如果您將文件複製到類似的路徑下localcommunity codepools,你將使用該類的定義。

+0

與Vinai的解決方案一起去吧,更優雅! – benmarks 2012-02-08 22:47:49

+0

感謝您解釋發生了什麼! – 2012-02-09 11:55:51

2

由於Magento 1.7可以使用方法Mage::app()->setRequest($request)替代controller_front_init_before事件的觀察者中的請求對象,如Vinai所建議的。

警告爲Magento企業:整個頁面緩存也不會用這種方法會奏效,因爲它依賴於改變controller_front_init_before做過請求對象。您可能需要手動將舊請求中的所有屬性複製到新的請求中 - 或者用benmarks solution替換請求類。

+1

Sali @so Fabian! – 2012-08-06 20:44:27