4

I am trying to overload/rewrite the core Onepagecontroller with my custom controller in local pool, but it is not working. I am using Magento 1.5.1

Here is my file structure and code:

Controller file: \app\code\local\Odc\Mycheckout\controllers\OnepageController.php

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

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

config.xml file: \app\code\local\Odc\Mycheckout\etc\config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Odc_Mycheckout>
                <version>0.0.1</version>
            </Odc_Mycheckout>
        </modules>
        <global>
            <controllers>
                <Mage_Checkout>
                    <rewrite>
                        <onepage>Odc_Mycheckout_Onepage</onepage>
                    </rewrite>
                </Mage_Checkout>
            </controllers>
        </global>
        <frontend>
            <routers>
                <mycheckout>
                   <args>
                        <modules>
                            <Odc_Mycheckout before="Mage_Checkout">Odc_Mycheckout</Odc_Mycheckout>
                        </modules>
                   </args>
                </mycheckout>
            </routers>
        </frontend>
    </config>

Odc_Mycheckout.xml file: \app\etc\module\Odc_Mycheckout.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Odc_Mycheckout>
          <active>true</active>
          <codepool>local</codepool>
        </Odc_Mycheckout>
      </modules>
    </config>
Uzair Ahmed
  • 259
  • 2
  • 14

2 Answers2

6

CamelCase strikes again.

Odc_Mycheckout.xml file: \app\etc\module\Odc_Mycheckout.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Odc_Mycheckout>
          <active>true</active>
          <codePool>local</codePool> <!-- Capital P in pool -->

Also, in your module config file:

    <frontend>
        <routers>
            <checkout> <!-- must match the router config you are trying to override -->
               <args>
                    <modules>
                        <Odc_Mycheckout before="Mage_Checkout">Odc_Mycheckout</Odc_Mycheckout>
                    </modules>
               </args>
            </checkout>
        </routers>
    </frontend>

EDIT:

To troubleshoot when rewritten controllers aren't working it can help to go back to basics. One approach is to use the same methods as Mage_Core_Controller_Varien_Router_Standard. In a script test.php:

<?php

ini_set('display_errors',1);
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app();

$module     = 'Odc_Mycheckout';
$controller = 'Onepage';

$router     = new Mage_Core_Controller_Varien_Router_Standard;
$filename   = $router->getControllerFileName($module,$controller);
$classname  = $router->getControllerClassName($module,$controller);

include $filename;

$controller = Mage::getControllerInstance(
    $classname,
    Mage::app()->getRequest(),
    Mage::app()->getResponse()
);

var_dump($controller); //should be a  class
benmarks
  • 23,384
  • 1
  • 62
  • 84
  • Thanks for the answer but still it is not working after given changes. Please also have a look at tag in config.xml. Is this a right way? – Uzair Ahmed Oct 25 '12 at 06:41
  • No, that xpath (`global/controllers`) will not be evaluated by any core Magento code. Try adding another action to your custom controller class and call *that* action via the URL. Also, note that the path to your declaration file should be *app/etc/modules/* not *app/etc/module/* – benmarks Oct 25 '12 at 11:28
  • Yes the declaration file is at app/etc/modules/. But through URL I am getting this Forbidden error: You don't have permission to access /onedaycheckout/app/code/local/Odc/Mycheckout/Checkout/controllers/sdasd.php on this server. – Uzair Ahmed Oct 25 '12 at 12:21
  • How is the request resolving to sdasd.php? That should not be possible with standard Magento. – benmarks Oct 25 '12 at 16:35
  • Sorry it was by mistake. The url was /onedaycheckout/app/code/local/Odc/Mycheckout/Checkout/controllers/OnepageController.php and showing the same Forbidden error. – Uzair Ahmed Oct 26 '12 at 04:36
  • That's not a URL! :-) Regarding the error message, make sure path is actually correct, check file permissions & ownership (I've seen permissions cause an issue before). See also my troubleshooting edit to the answer. – benmarks Oct 26 '12 at 11:43
  • @Uzair Ahmed, Could you update your code above with all the changes you made so far – MagePal Extensions Oct 26 '12 at 11:53
0

The below tags are not needed.

   <global>
        <controllers>
            <Mage_Checkout>
                <rewrite>
                    <onepage>Odc_Mycheckout_Onepage</onepage>
                </rewrite>
            </Mage_Checkout>
        </controllers>
    </global>