12

I need to overwrite action called _initAction from Controller:

/app/code/core/Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php

into my own module and controller:

/app/code/local/Qbikz/ToolTip/controllers/Adminhtml/Catalog/Product/AttributeController.php

I already tried different ways to solved it: First method where <from> and <to> I tried different combinations (Showing 404 page not found):

 <rewrite>
     <qbikz_tooltip_adminhtml_catalog_product_attribute>
         <from><![CDATA[#^/adminpanel/catalog_product_attribute/edit/#]]></from>
         <to>/tooltip/adminhtml_catalog_product_attribute/init/</to>
     </qbikz_tooltip_adminhtml_catalog_product_attribute>
</rewrite>

+

 <frontend>
        <routers>
            <qbikz_tooltip>
                <use>admin</use>
                <args>
                    <module>Qbikz_ToolTip</module>
                    <frontName>catalog_product_attribute</frontName>
                </args>
            </qbikz_tooltip>
        </routers>
    </frontend>

Second method(Page showing with ignore my rules):

<admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Qbikz before="Mage_Adminhtml_Catalog_Product">Qbikz_ToolTip_Adminhtml_Catalog_Product</Qbikz>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

In my controller I put die("ok"); But it's not showing up. Maybe somebody could help? Thanks.

15

Use the second method but a bit modified. Your XML should look like this:

<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <Qbikz_ToolTip before="Mage_Adminhtml">Qbikz_ToolTip_Adminhtml</Qbikz_ToolTip>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

Now create the file Qbikz/ToolTip/controllers/Adminhtml/Catalog/Product/AttributeController.php with this content:

<?php 
require_once 'Mage/Adminhtml/controllers/Catalog/Product/AttributeController.php';
class Qbikz_ToolTip_Adminhtml_Catalog_Product_AttributeController extends Mage_Adminhtml_Catalog_Product_AttributeController {
   protected function _initAction(){
       //your magic here
   }
}
  • @Marius what is better to require a file, your way or require_once (Mage::getModuleDir('controllers','Mage_Adminhtml').DS.'Catalog'.DS.'Product'.DS.'AttributeController.php'); additionally I want to know whether we can rewrite block and controller in a single module or we have to write separate modules to write block and than controller. – Adnan Nov 16 '16 at 4:24
  • Both ways have the same result. But if you want to be 100% politically correct use the method you mentioned. Also you can use the same module to rewrite blocks and controllers. – Marius Nov 16 '16 at 5:32
  • This seems not to work when trying to override indexAction() of Mage_Checkout :: CartController.php. – Max Mar 23 '17 at 12:03
  • In 1.9.4.2 this is throwing an error for me. The controller throws a 404. Perhaps there's a different way to do it now? It is happening to me when overriding a checkout controller – gabtzi Sep 18 at 11:36
0

In my onestepcheckout extension, configuration file: app/code/local/Gurutheme/Onestepcheckout/etc/config.xml

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

I added a controller

app/code/local/Gurutheme/Onestepcheckout/controllers/OnepageController.php

for overriding Onepage core controller

app/code/core/Mage/Checkout/controllers/OnepageController.php

This is basic source code of that overriding controller:

require_once("Mage/Checkout/controllers/OnepageController.php");

class Gurutheme_Onestepcheckout_OnepageController extends Mage_Checkout_OnepageController {

}

The main thing that when rewrite magento controllers, we should include the original one and extend from it, it helps all native behavior before so all pre-built features would work.

  • Only if you rely on additional (protected) methods of the original class in your overridden actions. All actions missing in your controller will be handled by the original controller. Note that you technically don't rewrite the controller but add it to the list of handlers for a front name – Fabian Schmengler Jan 25 '16 at 7:22
  • Totally agree, we actually re-route it but should extend the original one for inheriting any existing code – phanvugiap Jan 25 '16 at 7:24
-1

I rewrote enterprise wishlist controller.

In my config.xml

<frontend>
    <routers>
        <wishlist>
            <args>
                <modules>
                    <namespace_modulename before="Enterprise_Wishlist">Namespace_Modulename</namespace_modulename>
                </modules>
                <module>Namespace_Modulename</module>
                <frontName>shoppinglist</frontName>
            </args>
        </wishlist>
    </routers>
</frontend>

And in my IndexController Add below line:

require_once 'Enterprise/Wishlist/controllers/IndexController.php';

//before

class Namespace_Modulename_IndexController extends Enterprise_Wishlist_IndexController{}

You can also rewrite helper like this: https://magento.stackexchange.com/a/91474/33509

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.