Search code examples
phpmagentooverridingmagento-1.7mage

Controller override in Magento


Hello friend i need to over ride magento core controller

i want to override a Mage\ImportExport\controllers\Adminhtml\ExportController.php

In my community\compnyname\modulename\controllers\Adminhtml

this is my config.xml in \compnyname\modulename\etc

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <company_module>
            <version>1.0</version>
        </company_module>
    </modules>
    <global>
        <rewrite>
            <company_module>
                <from><![CDATA[#^/controller/adminhtml/#]]></from>
                <to>/controller/adminhtml/</to>
            </company_module>
        </rewrite>
    </global>
</config>

this is my controller code in \compnyname\modulename\controllers\Adminhtml\ExportController.php

<?php
require_once('/Mage/ImportExport/controllers/Adminhtml/ExportController.php');
class company_module_ImportExport_Controller_Adminhtml_ExportController extends Mage_ImportExport_Adminhtml_ExportController
{
    function indexAction()
    {
        echo "i am called";die;
    }
}
?>

Please help me in override a controller

Where is problem in my code?


Solution

  • A rewrite like that is deprecated since Magento 1.3 (2009). You can read more about it here.

    What you would want to do instead is like the following:

    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <companyname_exportproduct before="Mage_ImportExport_Adminhtml">Companyname_ExportProduct_Adminhtml</companyname_exportproduct>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    

    Your classname and require looks a bit off as well. And it's good practice to not end php class files with a php end tag since it could accidentally include a whitespace that would mess up sent headers order.

    So having said that I'd change your class file to:

    <?php
    require_once('Mage/ImportExport/controllers/Adminhtml/ExportController.php');
    class Companyname_ExportProduct_Adminhtml_ExportController extends Mage_ImportExport_Adminhtml_ExportController
    {
        function indexAction()
        {
            echo "i am called";die;
        }
    }
    

    And just in case you have forgot I'm including the file you would have to have in app/etc/modules to make your module active:

    <config>
        <modules>
            <Companyname_ExportProduct>
                <active>true</active>
                <codePool>community</codePool>
            </Companyname_ExportProduct>
        </modules>
    </config>