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

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

问题描述

我有扩展CreativeStyle CheckoutByAmazon安装在我的magento 1.7存储,我试图重写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
\CheckoutByAmazon
\controllers
\CheckoutController.php
\etc
\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>

(In 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');
        }

}
?>

(In 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.

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

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天全站免登陆