1

I'm trying to configure the finish function for module.php in zend, from what I understand you need to configure some sort of listener (in bootstrap I think) that will call the finish function and I can then execute code after its finished with the user request.

Can someone provide some example code to setup the module to call finish once it has finished the user request.

Thanks!

4

2 回答 2

6

您可以按照以下onBootstrap方法执行此操作Module.php

public function onBootstrap(MvcEvent $e)
{
    $em = $e->getApplication()->getEventManager();
    $em->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, array($this, 'doSomething'));
}

然后doSomething在您Module.php的中定义一个函数,如下所示:

public function doSomething(MvcEvent $e)
{
    // your code goes here
}

如果您在同一事件上附加了多个侦听器,您还可以为要执行的回调函数添加一些优先级,如下所示:

$em->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, array($this, 'doSomethingFirst'), 20);
$em->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, array($this, 'doAnotherThingLater'), 10);

较高优先级的值最早执行。(默认优先级为 1,允许负优先级。)

于 2013-05-10T03:21:37.760 回答
2

基本思想是将侦听器附加到事件,正如您正确指出的那样,可以在您的类的onBootstrap方法中执行此操作。Module以下内容应该可以帮助您开始...

public function onBootstrap(MvcEvent $e)
{
    $e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_FINISH, function ($e) {        
        // do something...
    });
}
于 2013-05-10T03:03:52.773 回答