Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the earliest event Magento dispatches?

Tags:

magento

I need to fire off an event, as early as possible in the dispatch process, when a customer visits a frontend controller. The request is to be analysed and will, potentially, be redirected to a different URL, justifying the "as early as possible" requirement.

What's the earliest event a developer can subscribe to?

like image 521
Nick Avatar asked Jun 20 '11 11:06

Nick


People also ask

What is dispatch event in Magento 2?

In Magento 2, events can be dispatched using the Magento\Framework\Event\Manager class. While developing a custom extension, we need to create a custom dispatch event, so in future, if any other developer needs to extend functionality at that time they can use this dispatch event.

What are Magento 2 events?

What are Magento 2 Events? Magento 2 events are implemented to run custom code in response to any particular Magento 2 event or custom event. Events are dispatched by modules when certain actions are triggered. When an event is dispatched, it can pass data to any observers configured to watch that event.

When the observers are executed in Magento?

Magento 2 Observers They are executed whenever a specific event for which they were set to listen is triggered. To create an observer in Magento 2, you must place your class file under the ModuleRoot/Observer directory.


3 Answers

The earliest event I'd rely on is

controller_front_init_before 

The Magento front controller object (distinct from the the "index.php front controller") is the object that managers the routers, which in turn manages the action controller dispatch.

There's a few others (resource_get_tablename, core_collection_abstract_load_before, core_collection_abstract_load_after) that fire before, but they're more of the side effect variety (Magento using it's own systems to bootstrap itself) than anything you'd want to rely on.

Finally, as should always be pointed out, if you do a little logging in app/Mage.php

public static function dispatchEvent($name, array $data = array())
{
    file_put_contents('/tmp/events.log',"$name \n",FILE_APPEND);
    Varien_Profiler::start('DISPATCH EVENT:'.$name);
    $result = self::app()->dispatchEvent($name, $data);
    #$result = self::registry('events')->dispatch($name, $data);
    Varien_Profiler::stop('DISPATCH EVENT:'.$name);
    return $result;
}

patterns begin to emerge.

like image 122
Alan Storm Avatar answered Oct 14 '22 02:10

Alan Storm


You are probably looking for controller_action_predispatch event.

There are a bunch of other events fired before it, but in this case I am assuming you still want to access the dispatched action controller.

If you want it even earlier, then your best bet would be controller_front_init_before, which is fired before the front controller is initialized.

like image 41
Daniel Sloof Avatar answered Oct 14 '22 04:10

Daniel Sloof


Here is a list of events in Magento 1.4.1.1 and 1.5.1.0

There is also an event for each of the controller_action_predispatch and controller_action_postdispatch. To know which actions are being called you can add the following line

Mage::log('controller_action_predispatch_'.$this->getFullActionName());

to the preDispatch function of app/code/core/Mage/Core/Controller/Varien/Action.php

A similar line can be added to postDispatch function in the same file.

like image 22
Ozair Kafray Avatar answered Oct 14 '22 02:10

Ozair Kafray