Module.php
· 987 B · PHP
Raw
<?php
// In a module class somewhere...
use Zend\EventManager\LazyListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$services = $app->getServiceManager();
$events = $app->getEventManager();
$listener = new LazyListener([
'listener' => YourShortCircuitingListener::class,
'method' => '__invoke',
], $services);
$events->attach(MvcEvent::EVENT_ROUTE, $listener, PHP_INT_MAX);
}
}
// And now for the listener...
class YourShortCircuitingListener
{
public function __invoke(MvcEvent $e)
{
$request = $e->getRequest();
if (/* some criteria that, if met, means we should continue normal operation */) {
return;
}
// At this point, we know we want to short-circuit
$response = $e->getResponse();
// populate the response
return $response;
}
}
| 1 | <?php |
| 2 | |
| 3 | // In a module class somewhere... |
| 4 | use Zend\EventManager\LazyListener; |
| 5 | use Zend\Mvc\MvcEvent; |
| 6 | |
| 7 | class Module |
| 8 | { |
| 9 | public function onBootstrap(MvcEvent $e) |
| 10 | { |
| 11 | $app = $e->getApplication(); |
| 12 | $services = $app->getServiceManager(); |
| 13 | $events = $app->getEventManager(); |
| 14 | |
| 15 | $listener = new LazyListener([ |
| 16 | 'listener' => YourShortCircuitingListener::class, |
| 17 | 'method' => '__invoke', |
| 18 | ], $services); |
| 19 | |
| 20 | $events->attach(MvcEvent::EVENT_ROUTE, $listener, PHP_INT_MAX); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // And now for the listener... |
| 25 | class YourShortCircuitingListener |
| 26 | { |
| 27 | public function __invoke(MvcEvent $e) |
| 28 | { |
| 29 | $request = $e->getRequest(); |
| 30 | if (/* some criteria that, if met, means we should continue normal operation */) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | // At this point, we know we want to short-circuit |
| 35 | $response = $e->getResponse(); |
| 36 | |
| 37 | // populate the response |
| 38 | |
| 39 | return $response; |
| 40 | } |
| 41 | } |