Created
August 17, 2017 20:02
-
-
Save weierophinney/41a3401bfe48ca6cef0a94e4ba41ee15 to your computer and use it in GitHub Desktop.
Demonstrates a zend-mvc listener that short-circuits, and how to register it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment