user2368163
user2368163

Reputation: 13

Configuring zend module to use finish

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!

Upvotes: 1

Views: 234

Answers (2)

Mohammad ZeinEddin
Mohammad ZeinEddin

Reputation: 1678

You can do this in the onBootstrap method of your Module.php as the following:

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

and then define a function that doSomething in your Module.php as the following:

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

You can also add some priority for the callback functions you want to execute if you attached more than one listener on the same event as the following:

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

Higher priority values execute earliest. (Default priority is 1, and negative priorities are allowed.)

Upvotes: 6

Crisp
Crisp

Reputation: 11447

The basic idea is to attach a listener to the event, as you've rightly noted the place to do that is in the onBootstrap method of your Module class. The following should get you started...

public function onBootstrap(MvcEvent $e)
{
    $e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_FINISH, function ($e) {        
        // do something...
    });
}

Upvotes: 2

Related Questions