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

查看:15
本文介绍了任何页面加载时的 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.

  • 第一个实际的单触发事件是 controller_front_init_before.此事件在 Front Controller 初始化时分派,以响应所有分派的请求.因为它是在调用动作控制器之前分派的,所以只有全局区域的观察者才能观察到这个事件.

  • 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.

假设请求从前端控制器通过路由器路由到动作控制器,有一些 preDispatch() 中渲染之前可以观察到的事件 - 注意通用的 controller_action_predispatch 可以为所有事件使用的事件句柄与两个动态事件句柄:

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:

    假设 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());
    

    在所有的路由、调度、视图配置、块实例化和渲染完成后,在发送响应之前,有一个由 Front Controller 调度的 last-ditch 事件: controller_front_send_response_before.此事件不适合您的需要,但它是开始此答案的 controller_front_init_before 事件的一个不错的书挡.

    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天全站免登陆