5

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

  • 2
    the problem is you don't have any method/function inside your controller class. You cannot expect an action inside a class to be executed without a method. So put you code inside public function indexAction(){...} or any other action you wanna override – Shathish Oct 21 '13 at 12:16
  • @Shathish Still I can't see the log.. Is that log message will be visible if I am in a specific page or just the home page? right now I had tried clicking all the pages. – Mr_Green Oct 21 '13 at 12:19
  • you're overriding category controller, so you need to visit a category page – Shathish Oct 21 '13 at 12:23
  • @Shathish there is only catalog module present.. there is no category page or module.. or I didn't understand you..? – Mr_Green Oct 21 '13 at 12:31
  • go to a category page/ product list page in frontend, for example: Electronics page / cellphones page – Shathish Oct 21 '13 at 12:36
19

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']));
    }
}
5

Your controller class will need a method that will be called in order to work proper. Also the path to include the original controller is not correct.

require_once Mage::getModuleDir('controllers', 'Mage_Catalog').DS.'CategoryController.php';
class Edge_AjaxCatalog_CategoryController extends Mage_Catalog_CategoryController
{
   public function viewAction()
   {
       Mage::log("controller..");
       parent::viewAction();
   }
}

For more on extending core controller check out How to extend Magento core controller by Inchoo

  • I did same what you mention above but still I can't see log message.. right now I am going through your link.. – Mr_Green Oct 21 '13 at 13:30
  • Did you also take a look at the article? <edge_ajaxcatalog before="Mage_Catalog"> should be <edge_ajaxcatalog before="Mage_Catalog_CategoryController">. Also check if no other extensions are overwriting the controller – Sander Mangel Oct 21 '13 at 13:41
  • @Mr_Green codepool should be with a capital p (codePool) – Sander Mangel Oct 21 '13 at 13:54
  • I updated my post with the changes you have mentioned and also referred the link.. still I can't see the log message.. updated post. I am even deleting var/cache and var/session files and also clearing browser cache frequently.. – Mr_Green Oct 21 '13 at 13:55
  • @Mr_Green please change codepool to codePool and turn off caching while developing please to make sure you see the changes – Sander Mangel Oct 21 '13 at 13:58
1

Frontend router should be:

<frontend>    
    <routers>
        <catalog>
            <args>
                <modules>
                    <Edge_AjaxCatalog before="Mage_Catalog">Edge_AjaxCatalog</Edge_AjaxCatalog>
                </modules>
            </args>
        </catalog>
    </routers>
</frontend>

And check if your module is working without catalog router rewrite.

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.