11

I need to set or update a cookie, but I want to ensure that all (or as much as possible) of the request processing occurs before my cookie generation code runs. i.e. if the user logs in, I want to ensure that the login processing has occurred before my code runs, or if the user adds something to their cart I want to know that all cart processing is done first.

Are there any events that are dispatched immediately before a response is sent to the browser?

11

The last event dispatched in Magento 1.x before content is rendered is

controller_front_send_response_after

If there are no extra requirements in observer data that you would need, this one should be perfect for you.

  • 3
    Actually, "controller_front_send_response_before" looks like what I need. Thanks for pointing me in the right direction! – Jim OHalloran Jan 24 '13 at 23:11
8

A handy trick, to find events fired during a page request / action, is to temporarily edit app/Mage.php and write out the events fired to var/log/system.log

 public static function dispatchEvent($name, array $data = array())
    {
        Varien_Profiler::start('DISPATCH EVENT:'.$name);
        $result = self::app()->dispatchEvent($name, $data);
        Varien_Profiler::stop('DISPATCH EVENT:'.$name);
        return $result;
    }

to

public static function dispatchEvent($name, array $data = array())
    {
        if(mage::getIsDeveloperMode()) {
           mage::log($name);
        }
        Varien_Profiler::start('DISPATCH EVENT:'.$name);
        $result = self::app()->dispatchEvent($name, $data);
        Varien_Profiler::stop('DISPATCH EVENT:'.$name);
        return $result;
    }

and then tail the log file. I have found this method extremely useful, and saved a lot of time hunting for that elusive event to use.

Naturally you should remove it immediately, as you do not want to commit changed core files. I wrap it into the developer check, just in case.

  • 2
    You can also enable the Profiler, it gives you extensive information of what happens in a page load. All events triggered are echoed there as well. – Rick Kuipers Jan 26 '13 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.