在社区扩展中覆盖 Magento 控制器 [英] Overriding a Magento Controller in community extension

查看:18
本文介绍了在社区扩展中覆盖 Magento 控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 magento 1.7 商店中安装了 CreativeStyle CheckoutByAmazon 扩展,我试图覆盖 CheckoutController 类,但 magento 似乎忽略了我的覆盖.任何建议将不胜感激.也清除了缓存但仍然不起作用

I have the extension CreativeStyle CheckoutByAmazon installed on my magento 1.7 store and I am trying to override the CheckoutController Class but magento seems to ignore my override. Any suggestions will be much appreciated. Also cleared cache but still doesnt work

(在应用程序/代码/本地文件夹中)

(In app/code/local Folder)

我的模块CheckoutByAmazon控制器CheckoutController.php等等config.xml

MyModule CheckoutByAmazon controllers CheckoutController.php etc config.xml

(app/etc/config.xml)

(app/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 中)

(In 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 中)

(In 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 将获取请求路径前端名称checkout",将其匹配到目录 ./app/code/core/Mage/Checkout/controllers/ 然后寻找匹配第二个和第三个参数作为文件、该文件中的类定义和该类中的操作方法的组合.非弃用的重写语法(您正试图用您的模块执行)只需添加一个额外的目录来匹配.在调试之前,最好先了解基础知识.这提供了调试控制器覆盖的好机会.

In order to begin debugging it's necessary to understand what you are trying to do. Without any custom modules in present, Magento will take the request path frontname "checkout", match it to the directory ./app/code/core/Mage/Checkout/controllers/ and then look for a match of the second and third parameters as a combination of file, class definition in that file, and action method in that class. Non-deprecated rewrite syntax (which you are trying to perform with your module) simply adds an additional directory for matching. Before debugging this, it's best to cover the basics. This presents a good opportunity to debug controller overrides.

必须满足许多先决条件(您的模块的 config.xml 合并、正确的语法和文件结构等).最好先测试这些是否正常工作,因为这将减少和确定我们需要调试的区域.

There are a number of preconditions which must be satisfied (your module's config.xml getting merged, correct syntax & file structure, etc.). It's good to test that these are working first, as it will reduce and pinpoint the areas where we need to debug.

在 Magento 根目录下的 test.php 文件中,执行以下操作:

In a test.php file in your Magento root, do the following:

<?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,您应该会看到您的控制器类名称.如果您看到错误,或者什么也没有看到,则说明相当低级别的某些内容已损坏.如果错误消息不直观,请检查您的模块配置是否正在合并并从那里移动.

Visit yoursite.com/test.php and you should see your controller class name. If you see an error, or you see nothing, something fairly low-level is broken. If the error message is unintuitive, check that your module config is being merged and move from there.

如果类实例化,我们知道以下内容:

If the class instantiates, we know the following:

  1. 基础模块配置很好
  2. 文件结构适合配置
  3. 明确的包含是好的

我怀疑你会走到这一步,因为我相信这个问题是基于配置的.有几种可能性.为了深入了解,将 test.php 修改为以下内容(假设 PHP >= 5.3):

My suspicion is that you will make it this far, because I believe the issue to be config-based. There are a few possibilities. To gain insight, amend test.php to the following (assuming 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));

输出应类似于以下内容:

The output should resemble something like the following:

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"
  }
  // ...
}

只要你看到不止一个子阵列,就会有一个控制器目录重写.

Wherever you see more than one subarray, there is a controller directory rewrite.

这篇关于在社区扩展中覆盖 Magento 控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆