Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Event On Any Page Load

Tags:

magento

I am wondering if there is an event that is fired once every time a page is loaded before rendering to html in magento?

This might be useful if you want to do some business logic for semi-static attributes that don't rely on user sessions.

For example I will be using this to deliver the canonical tag to the header of magento.

like image 348
ajameswolf Avatar asked Mar 25 '13 13:03

ajameswolf


People also ask

What is controller_ action_ predispatch Magento 2?

controller_action_predispatch_{route_name} – executes before controllers with the specific {route_name}. For example, to execute an observer before CMS pages controllers dispatching we should register our observer for the controller_action_predispatch_cms event.

What are Magento 2 events?

The events and observers implementation in Magento 2 is based on the publish-subscribe pattern. Using events and observers, you can run your custom code in response to a specific Magento event or even a custom event.

Where does the event name is referenced in your module directory?

Your unique event name is referenced in your module's events. xml file where you specify which observers will react to that event.


2 Answers

There are several request-related events which are dispatched for most page-/content-generating requests. Below is a partial list in processing order of some useful ones, and I expect others may comment on this post with some others. Many of these are not suitable for your need (I've noted in bold below where you should begin considering). There are also a few block-instantiation-related events which, although they could be observed for your purpose, are generic to every block and really aren't appropriate.

  • The first practical singly-fired event is controller_front_init_before. This event is dispatched in Front Controller initialization in response to all dispatched requests. Because it is dispatched before the action controllers are invoked, only global-area observers will be able to observe this event.

  • Assuming the request is routed from the Front Controller through the routers to an action controller, there are some events which can be observed prior to rendering in preDispatch() - note the generic controller_action_predispatch event handle which could be consumed for all events vs the two dynamic event handles:

    Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $this));
    Mage::dispatchEvent('controller_action_predispatch_' . $this->getRequest()->getRouteName(),
        array('controller_action' => $this));
    Mage::dispatchEvent('controller_action_predispatch_' . $this->getFullActionName(),
        array('controller_action' => $this));
    
  • How a response is being rendered may affect the events available; the main variations would come from whether or not layout updates are being used to render the response (and how). For example, core_layout_update_updates_get_after could be used to inject a layout update file to the list of configured module layout update files (a rare but potentially useful case). The controller actions are closely coupled with the layout modeling, so there are a few events which could work:

    • controller_action_layout_load_before
    • controller_action_layout_generate_xml_before
    • controller_action_layout_generate_blocks_before and controller_action_layout_generate_blocks_after - the latter of which would be the first applicable to your needs

Assuming that renderLayout() is being used in all actions about which you care, there are two events (one generic and one route-specific) which it dispatches:

    Mage::dispatchEvent('controller_action_layout_render_before');
    Mage::dispatchEvent('controller_action_layout_render_before_'.$this->getFullActionName());

After all of the routing, dispatching, view configuring, block instantiating, and rendering are done, there is one last-ditch event which is dispatched by the Front Controller before the response is sent: controller_front_send_response_before. This event is not suitable for your need, but it's a nice bookend to the controller_front_init_before event which began this answer.

like image 92
benmarks Avatar answered Oct 27 '22 19:10

benmarks


http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/ this will help.

app/code/core/Mage/Core/Controller/Varien/Action.php this event

controller_action_layout_load_before

is fired

app/code/core/Mage/Core/Block/Abstract.php event

core_block_abstract_to_html_before

above two events might be of help.

like image 41
Oscprofessionals Avatar answered Oct 27 '22 19:10

Oscprofessionals