What's the last event dispatched before content is sent to the browser?
-
16-10-2019 - |
سؤال
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?
المحلول
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.
نصائح أخرى
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.