PHP Magento Tips

How To Override Core Controllers In Magento

Rate this posting:
{[['']]}

Controller overrides in Magento can be devided into two parts. First one is admin controllers and second one is the frontend controllers.

Step 1:

In app/etc/modules/Foo_Bar.xml add below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Foo_Bar>
      <active>true</active>
      <codePool>local</codePool>
    </Foo_Bar>
  </modules>
</config> 

Step 2:

In app/code/local/Foo/Bar/etc/config.xml add below Code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Foo_Bar>
            <version>0.1.0</version>
        </Foo_Bar>
    </modules>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Foo_Bar before="Mage_Adminhtml">Foo_Bar_Adminhtml</Foo_Bar>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <frontend>
    <routers>
        <catalog>
        <args>
            <modules>
                <Foo_Bar before="Mage_Catalog">Foo_Bar_Catalog</Foo_Bar>
            </modules>
        </args>
        </catalog>
    </routers>
    </frontend>
</config> 

Here in Front-end we have rewrite catalog_category_view action. In fact, how can i know this action is called on front end on Category page ? You can easily find with some debugging tool in Magento, But one tricks that i am using is to look on the class that is appended to the body tag.

Step 3:

In app/code/local/Foo/Bar/controllers/Catalog/CategoryController.php add below code: 

<?php

// app/code/local/Foo/Bar/controllers/Catalog/CategoryController.php
require_once Mage::getModuleDir('controllers', 'Mage_Catalog') . DS . 'CategoryController.php';
class Foo_Bar_Catalog_CategoryController extends Mage_Catalog_CategoryController {
    
    public function viewAction() {
        die ("It Worked!");
    }
}
  
Step 4:

In app/code/local/Foo/Bar/controllers/Adminhtml/Catalog/ProductController.php add below code:

 <?php
// app/code/local/Foo/Bar/controllers/Adminhtml/Catalog/ProductController.php
require_once 'Mage/Adminhtml/controllers/Catalog/ProductController.php';

class Foo_Bar_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController
{
    public function newAction()
    {
        echo "We have rewritten core newAction"; 
        die;
    }
}

Now on Front End Refresh any Category page you will find our message.

Also go to backend and  go to Catalog --> Manage Product and click on Add Product, You will find new Message there also.
Tag : Magento
0 Comments On "How To Override Core Controllers In Magento"

Back To Top