Magento Event On Any Page Load

后端 未结 4 683
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 20:03

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 b

相关标签:
4条回答
  • 2020-12-13 20:42

    I think you are looking for this event controller_action_layout_render_before_RouteName_ControllerName_ActionName

    you can also log all the events which are fired on any page load from Mage class in below function

    public static function dispatchEvent($name, array $data = array())
        {
            Mage::log($name);
            Varien_Profiler::start('DISPATCH EVENT:'.$name);
            $result = self::app()->dispatchEvent($name, $data);
            Varien_Profiler::stop('DISPATCH EVENT:'.$name);
            return $result;
        }
    
    0 讨论(0)
  • 2020-12-13 20:44

    We can use controller_front_init_routers event using observer. In that observer method,you can get request object as follows.

    $request = $observer->getEvent()->getData('front')->getRequest();

    0 讨论(0)
  • 2020-12-13 20:47

    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.

    0 讨论(0)
  • 2020-12-13 20:48

    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.

    0 讨论(0)
提交回复
热议问题