Magento 1.8 – How to Override a Controller

controllersmagento-1.8overrides

I am trying to override a Mage controller which is in Mage/Catalog/controllers/CategoryController.php.

The folder structure which I created in trying to override the above controller is as follows:

--> app
   --> code
      --> core
         --> local
            --> Edge
               --> AjaxCatalog
                   --> controllers
                       --> CategoryController.php
                    --> etc
                       --> config.xml
  --> etc
      --> modules
          --> EdgeAjaxCatalog.xml

Edge/AjaxCatalog/controllers/CategoryController.php:

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Catalog').DS.'CategoryController.php';
class Edge_AjaxCatalog_CategoryController extends Mage_Catalog_CategoryController
{
   public function viewAction()
   {
       echo "alert('hi')"; //even tried Mage::log("controller..");
   }
}

Edge/AjaxCatalog/etc/config.xml

<config>
    <frontend>
        <routers>
            <catalog>
                <args>
                    <modules>
                         <edge_ajaxcatalog before="Mage_Catalog_CategoryController">Edge_AjaxCatalog</edge_ajaxcatalog>
                    </modules>
                </args>
            </catalog>
        </routers>
    </frontend>
</config>

Edge_AjaxCatalog.xml

<?xml version="1.0"?>
<!--we need to enable this module as any other if-->
<!--you wish to do it as standalone module extension-->
<config>
    <modules>
        <edge_ajaxcatalog>
            <active>true</active>
            <codePool>local</codePool>
        </edge_ajaxcatalog>
    </modules>
</config>

Still I can't see the log message in system.log file. (log is enabled)

what should I do more to override this controller?

Please help

Best Answer

In my humble opinion:

Rewriting controllers is recipe for disaster and makes code management a nightmare. Every plugin developer (or every store developer / systems integrator) truly believes that there will never be another plugin rewriting the same controller. The fact is, though, that one day it will happen (in the case of CartController.php it happens way too often). In my experience you then have to rewrite two plugins to create a chained inheritance. This is non-optimal.

So. In my opinion, instead, you should always use controller dispatch events. In your case the event looks something like this:

<?xml version="1.0"?>
<config>
    <global>
        <events>
            <controller_action_predispatch_catalog_category_view>
                <observers>
                    <yourcompany_capccv_predispatch>
                        <class>YourCompany_YourModule_Model_Observer</class>
                        <method>catalogCategoryViewPredispatch</method>
                    </yourcompany_capccv_predispatch>
                </observers>
            </controller_action_predispatch_catalog_category_view>
        </events>
    </global>
</config>

And the observer model:

<?php

class YourCompany_YourModule_Model_Observer
{

    public function catalogCategoryViewPredispatch($observer)
    {
        $helper = Mage::helper('core');
        $controller = $observer->getEvent()->getControllerAction();

        Mage::log("this works");

        //you can intercept and massage data on the request object
        $request = $controller->getRequest();
        $params = $request->getParams();

        $request->setParam('escapeme', $helper->escapeHtml($params['escapeme']));
    }
}
Related Topic