Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Demonstrates a zend-mvc listener that short-circuits, and how to register it
// 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 */) {
// 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