Magento 1.6.2 - Problems overriding contacts controller

Magento 1.6.2 - Problems overriding contacts controller

September 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 1.6.2 - Problems overriding contacts controller

Solving Magento 1.6.2 Controller Overriding Issues

Magento is a powerful e-commerce platform that allows for extensive customization through module development. However, when it comes to overriding built-in controllers like the Contacts controller in Magento 1.6.2, developers often face challenges. Let’s dive into your issue and find a solution.

Understanding the Issue

You’ve set up a new module MyNamespace_ContactsPlus to extend functionality on the contacts page by adding a newsletter sign-up option. Your setup looks correct at first glance:

  • You have created an index controller that extends the core Contacts index controller.
  • You’ve configured your module in the config.xml file, setting up rewrites and a new router for your custom controller.
  • The module is enabled via the MyNamespace_All.xml configuration.

However, you’re encountering a fatal error on the contacts page:

Fatal error: Call to a member function setFormAction() on a non-object in /srv/www/foo.com/app/code/core/Mage/Contacts/controllers/IndexController.php on line 54

This occurs when trying to call the setFormAction() method on an object that is not an instance of a block.

Identifying the Problem

The issue arises because you’re not properly loading the parent class in your index controller. When controllers are not autoloaded, you need to manually include them using require_once. However, in your current setup:

require_once 'Mage/Contacts/controllers/IndexController.php';

You’re requiring the core file directly, which is incorrect. Magento uses an autoloading system that should be respected.

The Correct Approach

To properly override a controller in Magento 1.x, you need to follow these steps:

  1. Create your module and controllers directory structure: Ensure your module follows the correct code pool and directory structure under app/code/local.

  2. Override the core class correctly: When extending a core class, always use the $this->___construct() method of the parent class to ensure all initialization methods are called properly.

  3. Configure your module properly: Make sure your config.xml file includes the correct rewrite and router configurations for your custom controller.

The Solution

Given your current setup, the issue is not in the configuration but in how you’re extending the core index controller. Here’s a corrected version of your index controller:

# app/code/local/MyNamespace/ContactsPlus/controllers/IndexController.php

require_once Mage::getModuleDir('controllers', 'Mage_Contacts');
class MyNameSpace_ContactsPlus_Contacts_IndexController extends Mage_Contacts_IndexController
{
    public function __construct()
    {
        parent::__construct();
        error_log('Yes, I did it!');
    }

    # Overloaded indexAction
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
}

In this corrected version:

  • We’re using Mage::getModuleDir('controllers', 'Mage_Contacts') to get the correct path for the core module’s controllers directory.
  • We’re calling the parent constructor inside your own __construct() method.
  • The indexAction() is overloaded and calls loadLayout() and renderLayout() methods directly.

Remember, when overriding controllers in Magento 1.x, it’s crucial to respect the platform’s auto-loading system and properly extend core classes using their constructors. This ensures that all necessary initializations are performed before your customizations take effect.

Conclusion

Overriding controllers in Magento 1.6.2 requires a deep understanding of the platform’s class loading mechanism. Always ensure you’re respecting the auto-loading system when extending core classes, and use the correct method to include parent classes.

Don’t forget to review the Magento Developer Guide for more detailed information on module development best practices and techniques.

If you found this solution helpful, feel free to share your experiences or any additional insights in the comments below. Happy coding!

Magento 1 Indexing Extensions Configuration

Our network