Extend magento core controller (Checkout/OnepageCo

2019-02-25 01:11发布

问题:

I am having problems while overriding a core controller. I want to add a new function but it only works if I do it in the core file (code/core/checkout/controllers/onepagecontroller.php).

I have followed some post, but it's not working. Some of them are:

  • http://www.magentocommerce.com/boards/viewthread/32979/P0/
  • http://www.webspeaks.in/2011/03/override-controllers-in-magento.html
  • www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_overload_a_controller

(I can't add more links, sorry)

I don't know what is happening... maybe you can help me ;).

I'm using magento 1.5 and I have this 3 files:


local -> Arias -> CoreExtended -> etc -> config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Arias_CoreExtended>
            <version>0.1.0</version>
        </Arias_CoreExtended>
    </modules>

    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Arias_CoreExtended before="Mage_Checkout">Arias_CoreExtended_Checkout</Arias_CoreExtended>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

app -> etc -> modules -> Arias_CoreExtended.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Arias_CoreExtended>
            <active>true</active>
            <codepool>local</codepool>
        </Arias_CoreExtended>
    </modules>
</config>

local -> Arias -> CoreExtended -> controllers -> Checkout -> OnepageController.php

<?php

require_once 'Mage/Checkout/controllers/OnepageController.php';

class Arias_CoreExtended_Checkout_OnepageController extends Mage_Checkout_OnepageController
{
    public function collectAction()
    {
        echo 'WTF?';
    }

    public function indexAction()
    {
        echo "This controller has been overridden.";
    }
}

Thanks in advance for your time, regards.

回答1:

I would first mirror the same directory structure of the controller you are overwriting, so in this case change: local/Arias/CoreExtended/controllers/Checkout/OnepageController.php to local/Arias/CoreExtended/controllers/OnepageController.php

You should lowercase your namespace/module name and you need to remove _Checkout as it is overwriting the controllers in general, and will look up any that exist in the module to use them instead if not fall back to standard. The correct code would be:

<arias_coreextended before="Mage_Checkout">Arias_CoreExtended</arias_coreextended>

I have used this exact setup with success to overwrite the Onepage controller!



回答2:

I would try lower casing your namespace/modulename like so:

<arias_coreextended before="Mage_Checkout">Arias_CoreExtended_Checkout</arias_coreextended>



回答3:

Your approach is mostly correct @satumo. The only thing you should change is this line

<Arias_CoreExtended before="Mage_Checkout">Arias_CoreExtended</Arias_CoreExtended>

So your full configuration have to look like this:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Arias_CoreExtended>
            <version>0.1.0</version>
        </Arias_CoreExtended>
    </modules>  

    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Arias_CoreExtended before="Mage_Checkout">Arias_CoreExtended</Arias_CoreExtended>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>