Add events for before/after a action of a controller in Magento

Add events for before/after a action of a controller in Magento

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add events for before/after a action of a controller in Magento

Adding Events for Before/After an Action in Magento Controller

Hello developers! Today we’re going to explore how to add before and after events to an action within your Magento controller. This is a common requirement when you need to perform additional logic or operations around the execution of your primary controller method.

Understanding the Current Controller

First, let’s take a look at the given controller:

#File: ./app/local/FilFact/Test/IndexController
class FilFact_Test_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        $this->__testConfig();
    }
}

We have an indexAction method that simply calls the __testConfig method. We want to add events for both before and after this indexAction.

Adding a Before Event

To add a “before” event, you’ll need to use Magento’s observer pattern. Here are the steps:

  1. Create an Observer

In your module (let’s say it’s called FilFact_Test), create an observer class under app/code/local/FilFact/Test/Model/Observer. You can name it something like IndexActionBeforeObserver:

class IndexActionBeforeObserver{
    public function beforeIndexAction(Varien_Event_Observer $observer){
        // Put your logic here that needs to run before the index action.
        Mage::log('Running before index action');
    }
}
  1. Configure the Observer

Next, open your module’s config.xml file and add a new <events> section for this observer:

<config>
    <global>
        <models>
            <!-- other configurations -->
        </models>

        <events>
            <controller_action_predispatch_standard_index_index>  <!-- notice the 'before' event name format -->
                <observers>
                    <filfact_test_indexaction_beforeobserver>
                        <type>singleton</type>
                        <class>FilFact_Test_Model_Observer_IndexActionBeforeObserver</class>
                        <method>beforeIndexAction</method>
                    </filfact_test_indexaction_beforeobserver>
                </observers>
            </controller_action_predispatch_standard_index_index>
        </events>

    </global>
</config>

Adding an After Event

Adding an “after” event follows a similar process:

  1. Create the Observer

Under app/code/local/FilFact/Test/Model/Observer, create another observer, e.g., IndexActionAfterObserver:

class IndexActionAfterObserver{
    public function afterIndexAction(Varien_Event_Observer $observer){
        // Put your logic here that needs to run after the index action.
        Mage::log('Running after index action');
    }
}
  1. Configure the Observer in config.xml

Add another <events> section for this observer:

<event>
  <name>controller_action_postdispatch_standard_index_index</name> <!-- notice the 'after' event name format -->
  <observers>
    <filfact_test_indexaction_afterobserver>
      <type>singleton</type>
      <class>FilFact_Test_Model_Observer_IndexActionAfterObserver</class>
      <method>afterIndexAction</method>
    </filfact_test_indexaction_afterobserver>
  </observers>
</event>

Conclusion

That’s it! Now, whenever the indexAction method is executed in your controller, Magento will trigger the observers you’ve configured before and after this action. This allows you to add additional functionality without modifying the core controller.

Remember to clear your cache (mage cache:clean) for these changes to take effect.

Note: In the real world, it’s recommended to use namespaces and autoloading instead of the legacy Magento class structure. However, this example follows the traditional Magento 1 approach for better understanding.

For more advanced Magento development practices, I recommend checking out our Magento Developer Guide at Laravel Company. Thanks for reading!

Indexing Extensions Configuration

Our network