Zend Framework:如何在将最终视图输出发送到浏览器之前利用它?
我试图在最终的 xhtml 输出作为字符串发送到浏览器之前访问它。动作和插件的 postDispatch() 方法似乎还为时过早。当我使用调试器单步执行 Zend_Controller_Front::dispatch()
方法时,我可以在 $this->_response->sendResponse()
之前以字符串形式访问所需的输出code> 在最后通过添加监视表达式 $this->getResponse()->getBody()
来调用。然而,那里似乎没有专门的钩子可以利用。我需要最终的响应正文作为字符串,以便将其发送到 Prince XML 以生成 pdf。有谁知道一种优雅的方法来做到这一点?
谢谢,阿德里安
I'm trying to access the final xhtml output right before it's sent to the browser as a string. The postDispatch() methods of the actions and plugins seem to be too early to do this. When I step through Zend_Controller_Front::dispatch()
method using a debugger, I can access the desired output as a string right before $this->_response->sendResponse()
is called at the very end by adding a watch expression $this->getResponse()->getBody()
. However, there seems to be no dedicated hook to tap into right there. I need the final response body as a string in order to send it to Prince XML to generate a pdf. Does anybody know an elegant way to do this?
Thanks, Adrian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Zend_Controller_Front
插件挂钩如下(来自此处):routeStartup()
在调用路由器上的Zend_Controller_Front
之前调用,以根据注册的路由评估请求。routeShutdown()
在路由器完成路由请求后调用。dispatchLoopStartup()
在Zend_Controller_Front
进入其调度循环之前被调用。preDispatch()
在调度程序调度操作之前调用。此回调允许代理或过滤器行为。通过更改请求并重置其分派标志(通过 Zend_Controller_Request_Abstract::setDispatched(false)),可以跳过和/或替换当前操作。postDispatch()
在调度程序调度操作后调用。此回调允许代理或过滤器行为。通过更改请求并重置其分派标志(通过 Zend_Controller_Request_Abstract::setDispatched(false)),可以为分派指定新的操作。dispatchLoopShutdown()
在 Zend_Controller_Front 退出其调度循环后被调用。所以
dispatchLoopShutdown()
是你要走的钩子 - 这是最后一件事Zend_Controller_Front::dispatch()
在返回或发送响应之前执行。另一种选择可能是使用
Zend_View
过滤器,即使它们是为完全不同的东西而设计的。这些过滤器可以添加到Zend_View
实例中,并在Zend_View::render()
中调用。过滤器只是一个提供filter($buffer)
方法的类的实例,该方法返回过滤后的$buffer
。但是使用这个接口来处理与过滤输出无关的事情,实际上似乎不是正确的方法。我个人认为,dispatchLoopShutdown() 插件将是正确的选择。
The
Zend_Controller_Front
plugin hooks are the following (from here):routeStartup()
is called beforeZend_Controller_Front
calls on the router to evaluate the request against the registered routes.routeShutdown()
is called after the router finishes routing the request.dispatchLoopStartup()
is called beforeZend_Controller_Front
enters its dispatch loop.preDispatch()
is called before an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (viaZend_Controller_Request_Abstract::setDispatched(false)
), the current action may be skipped and/or replaced.postDispatch()
is called after an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (viaZend_Controller_Request_Abstract::setDispatched(false)
), a new action may be specified for dispatching.dispatchLoopShutdown()
is called after Zend_Controller_Front exits its dispatch loop.So
dispatchLoopShutdown()
is your hook to go - it's the last thingZend_Controller_Front::dispatch()
does before returning or sending the response.Another option could be, even though they were designed for something completely different, to use
Zend_View
filters. These filters can be added to theZend_View
-instance and are called inZend_View::render()
. A filter is simply an instance of a class that provides afilter($buffer)
-method that returns the filtered$buffer
. But using this interface for something not related to filtering the ouptut, seems not to be the correct way actually.I personally think, that a
dispatchLoopShutdown()
-plugin will be the way to go.这里有一个关于如何使用视图过滤器的示例
http://matthewturland.com /2008/06/04/output-filters-in-zend_view/
Here a sample on how to use view filters
http://matthewturland.com/2008/06/04/output-filters-in-zend_view/