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?
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? 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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With