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

查看:53
本文介绍了在社区扩展中覆盖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)

MyModule \ Checkout通过亚马逊 \控制器 \ 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天全站免登陆