我如何覆盖Mage_Core_Controller_Request_Http

And*_*sch 2 overriding controller magento

我对Mage_Core_Controller_Request_Http进行了一些更改,但是在使用magento分发的文件中.这不是最好的方法,我知道,但我还没有弄清楚如何覆盖Controller目录中的文件.我可以找到如何覆盖controllers目录中的文件.

任何人都可以告诉我如何在我自己的扩展中覆盖Mage_Core_Controller_Request_Http.

谢谢

Vin*_*nai 9

如果您不想恢复到包含路径hack,还可以使用反射在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()));
}
Run Code Online (Sandbox Code Playgroud)

创建类Your_Module_Controller_Request_Http并扩展原始类Mage_Core_Controller_Request_Http.
在该事件之后,将使用您的请求对象而不是原始请求对象.

这使您可以尽可能保持升级安全,因为您不必从corcode池中复制完整的类.


ben*_*rks 5

编辑:Vinai的解决方案是更好的解决方案.

因为这个类是直接实例化的,所以你必须使用所谓的include path hack来覆盖它.

Varien_Autoload设置影响工作的包含路径的优先顺序app/Mage.php.该命令如下:

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

因此,如果将文件复制到localcommunity代码池下的类似路径,则将使用该类的定义.