Overriding a Magento Controller in community extension

余生长醉 提交于 2019-11-28 10:31:34
benmarks

tl;dr: The original extension config is likely the problem, but here's how to troubleshoot.

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.

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.

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);

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. Base module config is good
  2. File structure is appropriate for the config
  3. The explicit include is good

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.

To complete C4rter's answer, here is the full code you have to add into your config.xml file :

<admin>
    <routers>
        <checkoutbyamazon>
            <args>
                <modules>
                    <MyModule_CheckoutByAmazon before="Creativestyle_CheckoutByAmazon_Adminhtml">MyModule_CheckoutByAmazon</MyModule_CheckoutByAmazon>
                </modules>
            </args>
        </checkoutbyamazon>
    </routers>
</admin>

You don't need the frontend rewrite part.

Tried to do the same and failed also. But I found the reason. The CheckoutByAmazon Extension has an admincontroller of the same name:

<admin>
    <routers>
        <checkoutbyamazon>
            <use>admin</use>
            <args>
                <module>Creativestyle_CheckoutByAmazon</module>
                <frontName>checkoutbyamazon</frontName>
            </args>
        </checkoutbyamazon>
    </routers>
</admin>

So you have to rewrite the admin controller:

before="Creativestyle_CheckoutByAmazon_Adminhtml"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!