문제

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

도움이 되었습니까?

해결책

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

다른 팁

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top