在我的应用程序中,我需要扫描生成的输出以查找某些元素。看起来像SendResponseEvent
在这里会非常有帮助,因为它为我提供了生成的内容,就像发送的内容一样。不幸的是,文档没有给我任何有关如何实际监听此事件的线索。我试过这个:
class Module implements InitProviderInterface
{
public function init(ModuleManagerInterface $moduleManager) {
$em = $moduleManager->getEventManager();
$em->attach(SendResponseEvent::EVENT_SEND_RESPONSE, function(SendResponseEvent $e) {
$content = $e->contentSent();
/* work on $content */
$e->setContentSent($content);
});
}
}
但是,似乎监听器函数甚至没有被调用。
或者 Zend 2 中是否有更好的方法可以在发送 HTML 之前对其进行处理?
最佳答案
sendResponse
事件实际上是由 \Zend\Mvc\SendResponseListener
触发的,因此您实际上需要监听该类引发的事件。您无法通过附加到模块管理器事件管理器来做到这一点,而是需要通过附加到共享事件管理器来做到这一点,您可以从模块管理器事件管理器中获取该共享事件管理器...
class Module implements InitProviderInterface
{
public function init(ModuleManagerInterface $moduleManager)
{
$em = $moduleManager->getEventManager();
// get the shared manager
$shared = $em->getSharedManager();
// listen to the Zend\Mvc\SendResponseListener
$shared->attach('Zend\Mvc\SendResponseListener',
SendResponseEvent::EVENT_SEND_RESPONSE,
function(SendResponseEvent $e) {
$content = $e->contentSent();
/* work on $content */
$e->setContentSent($content);
});
}
}
关于php - 如何使用Zend2的SendResponseEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793798/