Overwriting / Overloading a controller class in Magento

Overwriting / Overloading a controller class in Magento

August 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Overwriting / Overloading a controller class in Magento

Overriding / Overloading the CategoryController Class in Magento

Hello developers! I often get asked how to properly override or overload controller classes in Magento without running into those pesky 404 errors. In this post, we’ll go through a step-by-step guide on how to achieve this for the CategoryController class.

Why Override Controllers?

Overriding controllers allows us to modify their behavior and add custom functionality without touching the core files directly. This ensures our changes remain intact during Magento updates and makes our code more maintainable.

Step 1: Create a New Module

First, create a new module in app/code/local. I’ll use “LHM_CategoryLanding” as an example:

  • Create directories: LHM/CatalogLanding/etc, LHM/CatalogLanding/Helper
  • Inside etc, create config.xml with the following content:
<config>
    <modules>
        <LHM_CategoryLanding>
            <version>0.1.0</version>
        </LHM_CategoryLanding>
    </modules>
    <!-- ... -->
</config>

This config file declares our new module and enables it in the frontend.

Step 2: Create the Controller

Next, create the controller directory structure:

  • app/code/local/LHM/CatalogLanding/controllers
  • Inside controllers, create CategoryController.php
// This is needed since Varien used a layout that is not easily auto-loadable
require_once("Mage/Catalog/controllers/CategoryController.php");

class LHM_CategoryLanding_Catalog_CategoryController extends Mage_Catalog_CategoryController
{
    // ... (keep the original methods)
}

We’re extending Mage_Catalog_CategoryController and leaving most of its methods intact. This approach reduces the risk of breaking core functionality.

Step 3: Override the ViewAction Method

Now let’s override the viewAction() method to add custom logic:

public function viewAction()
{
    if ($category = $this->_initCategory()) {
        // ... (keep the original code)
        /* START ===== Pete T additional code. Need to put this in override!! */
        if($category->getLevel()==2){
            $category->setDisplayMode('PAGE');
        }
        /* END ======= */
        // ... (rest of the method)
    } else {
        // ...
    }
}

In this example, we’re checking if the category level is 2 and setting its display mode to ‘PAGE’ accordingly. This custom logic won’t be in the core CategoryController.

Step 4: Register the Override

Finally, register our override in app/etc/modules/LHM_CategoryLanding.xml:

<config>
    <modules>
        <LHM_CategoryLanding>
            <active>true</active>
            <codePool>local</codePool>
        </LHM_CategoryLanding>
    </modules>
</config>

Save this file, then refresh your cache under System > Cache Management.

Troubleshooting

If you’re still encountering 404 errors, double-check the following:

  • Ensure “Mage/Catalog/controllers/CategoryController.php” is included correctly.
  • Check your module’s config.xml file for any syntax errors or incorrect handle names.
  • Make sure your cache is cleared after making changes.

Remember, overriding controllers requires a careful approach to avoid breaking core functionality. Always test thoroughly and keep backups of your files!

For more advanced Magento development techniques, check out our guides on Magento 2 Development Best Practices at LaravelCompany.

Happy coding!

Catalog Extensions Themes Configuration

Our network