任何页面加载上的Magento事件 [英] Magento Event On Any Page Load

查看:83
本文介绍了任何页面加载上的Magento事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个事件,在将页面渲染成magento中的html之前,每次加载页面都会触发一次?

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.

例如,我将使用它来将规范标签传递到magento的标头.

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

推荐答案

存在与几个请求相关的事件,这些事件被分派给大多数生成页面/内容的请求.以下是按处理顺序排列的部分有用列表,我希望其他人可以和其他一些人一起评论这篇文章.其中许多不适合您的需求(我已在下面以粗体显示的位置应开始考虑).还有一些与块实例化相关的事件,尽管出于您的目的可以观察到它们,但它们对于每个块都是通用的,实际上是不合适的.

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));

  • 响应的呈现方式可能会影响可用事件;主要变化将来自是否使用布局更新来呈现响应(以及响应方式).例如, core_layout_update_updates_get_after 可用于将布局更新文件注入到已配置的模块布局更新文件列表中(一种罕见但可能有用的情况).控制器的动作与布局建模紧密相关,因此有一些事件可以起作用:

  • 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

    假设您要执行的所有操作中都使用了renderLayout(),则有两个事件(一个是通用事件,一个是特定于路由的事件)

    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.

    这篇关于任何页面加载上的Magento事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆