Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created August 17, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weierophinney/41a3401bfe48ca6cef0a94e4ba41ee15 to your computer and use it in GitHub Desktop.
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
<?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