我在我的 magento 1.7 商店上安装了扩展 CreativeStyle CheckoutByAmazon,我试图覆盖 CheckoutController 类,但 magento 似乎忽略了我的覆盖。任何建议将不胜感激。也清除了缓存但仍然不起作用
(在 app/code/local 文件夹中)
我的模块
\结帐由亚马逊
\ Controller
\结帐 Controller .php
\ETC
\config.xml
(应用程序/etc/config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyModule_CheckoutByAmazon>
<version>0.1.0</version>
</MyModule_CheckoutByAmazon>
</modules>
<frontend>
<routers>
<checkoutbyamazon>
<args>
<modules>
<MyModule_CheckoutByAmazon before="Creativestyle_CheckoutByAmazon">MyModule_CheckoutByAmazon</MyModule_CheckoutByAmazon>
</modules>
</args>
</checkoutbyamazon>
</routers>
</frontend>
</config>
(在 app/code/local/MyModule/CheckoutByAmazon/controllers/CheckoutController 中)
<?php
//Controllers are not autoloaded so we will have to do it manually:
require_once 'Creativestyle/CheckoutByAmazon/controllers/CheckoutController.php';
class MyModule_CheckoutByAmazon_CheckoutController extends Creativestyle_CheckoutByAmazon_CheckoutController {
public function indexAction() {
exit('I am here');
parent::indexAction();
}
//i want to override this function
public function saveShippingAction() {
//insert my overriding code
exit('saveShipping');
}
}
?>
(在 etc/modules/MyModule_CheckoutByAmazon.xml 中)
<?xml version="1.0"?>
<config>
<modules>
<MyModule_CheckoutByAmazon>
<active>true</active>
<codePool>local</codePool>
</MyModule_CheckoutByAmazon>
</modules>
</config>
我哪里错了?
最佳答案
tl;dr:原始扩展配置可能是问题所在,但这里是如何排除故障。
为了开始调试,有必要了解您正在尝试做什么。目前没有任何自定义模块,Magento 将采用请求路径 frontname “checkout”,将其与目录 ./app/code/core/Mage/Checkout/controllers/匹配,然后查找第二个和第三个参数的匹配项为文件、该文件中的类定义和该类中的操作方法的组合。不推荐使用的重写语法(你试图用你的模块执行)只是为匹配 添加了一个附加目录.在调试之前,最好先了解一下基础知识。这为调试 Controller 覆盖提供了一个很好的机会。
必须满足许多先决条件(您的模块的 config.xml 被合并、正确的语法和文件结构等)。最好先测试这些是否正常工作,因为它将减少并确定我们需要调试的区域。
在 Magento 根目录下的 test.php 文件中,执行以下操作:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL^E_STRICT);
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app();
$origDir = Mage::getModuleDir('controllers','Creativestyle_CheckoutByAmazon').DS;
require_once $origDir.'CheckoutController.php';
$controller = new MyModule_CheckoutByAmazon_CheckoutController(
Mage::app()->getRequest(), //required constructor arg
Mage::app()->getResponse() //required constructor arg
);
echo get_class($controller);
访问 yoursite.com/test.php,您应该会看到您的 Controller 类名称。如果你看到一个错误,或者你什么也没看到,那么一些相当低级别的东西就被破坏了。如果错误消息不直观,请检查您的模块配置是否正在合并并从那里移动。
如果类实例化,我们知道以下内容:
我怀疑你会做到这一点,因为我相信这个问题是基于配置的。有几种可能性。为了深入了解,请将 test.php 修改为以下内容(假设 PHP >= 5.3):
<?php
ini_set('display_errors',1);
error_reporting(E_ALL^E_STRICT);
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app();
$router = Mage::app()->getFrontController()->getRouter('standard');
/* @var $router Mage_Core_Controller_Varien_Router_Standard */
$reflection = new ReflectionClass($router);
$modules = $reflection->getProperty('_modules');
$modules->setAccessible(true);
var_dump($modules->getValue($router));
输出应类似于以下内容:
array(35) {
["core"]=>
array(1) {
[0]=>
string(9) "Mage_Core"
}
["install"]=>
array(1) {
[0]=>
string(12) "Mage_Install"
}
["directory"]=>
array(1) {
[0]=>
string(14) "Mage_Directory"
}
// ...
["checkout"]=>
array(2) {
[0]=>
string(25) "MyModule_CheckoutByAmazon"
[1]=>
string(13) "Mage_Checkout"
}
// ...
}
无论您在哪里看到多个子数组,都会重写 Controller 目录。
关于magento - 在社区扩展中覆盖 Magento Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15814708/