Zend 框架 - 在呈现布局之前执行的最后一段代码

2021-12-29 00:00:00 php zend-framework zend-view zend-layout

我想在渲染布局之前执行一些代码,然后执行所有其他代码.我会把那个代码放在哪里?

我特别想在布局使用 headLink、headScript 和 inlineScript 视图助手中引用的文件之前对其进行修改.

以下是我要采取的步骤:

  1. 循环那些视图助手中的文件
  2. 列出本地文件
  3. 从视图助手中删除本地文件
  4. 将本地文件列表作为参数引用到服务器脚本,该脚本将它们组合起来用于单个 HTTP 请求
  5. 将新的组合脚本引用添加到适当的视图助手

<小时>

前端控制器插件似乎不会帮助我实现这一点,原因如下:

  1. postDispatch() 在执行每个控制器操作后执行,我需要样式表/javascripts 的完整列表
  2. dispatchLoopShutdown() 在控制器动作循环后执行,但此时布局已经呈现

<小时>

正如 Rufinus 所建议的,我通过扩展视图助手解决了这个问题.(镜像)

您可以通过插件访问的最后一部分应该是 dispatchLoopShutdown

ZendFramework2 参见 http://zendframework2.de/en/cheat-sheet.html或 这个 gdoc

I want to execute some code right before the layout is rendered, after all other code is executed. Where would I put that code?

I am specifically trying to modify the files referenced in the headLink, headScript, and inlineScript view helpers before they're used by the layout.

Here are the steps I want to take:

  1. Loop over the files in those view helpers
  2. Make a list of the local files
  3. Remove local files from the view helpers
  4. Reference the local file list as a parameter to a server script that combines them for a single HTTP request
  5. Add that new combine script reference to the appropriate view helper


It doesn't appear that a Front Controller Plugin is going to help me accomplish this, and here's why:

  1. postDispatch() gets executed after every controller action that's executed, and I need the full list of stylesheets/javascripts
  2. dispatchLoopShutdown() gets executed after the controller action loop, but the layout has already been rendered at this point


As Rufinus suggested, I solved this by extending the View Helpers. My question from that angle as well as the solution is here.

解决方案

see Orginal PDF created by Thorsten Ruf (Mirror)

the very last part you can access via plugin should be dispatchLoopShutdown

EDIT: For ZendFramework2 see http://zendframework2.de/en/cheat-sheet.html or this gdoc

相关文章