In your layout listener you'll need to apply some conditional logic to determine if the view model your controller returned is terminal before setting a layout.
This can be done by getting the result from the MvcEvent
in your listener and checking it's a ViewModel
instance that doesn't terminate()
public function yourLayoutListenerCallback(MvcEvent $e)
{
// get the dispatched result from the event
$result = $e->getResult();
// check it's a view model that doesn't terminate
if (!$result instanceof \Zend\View\Model\ViewModel || $result->terminate()) {
// do nothing
return;
}
// your existing layout logic ....
}
Note: you'll need to make sure your layout listener has a low enough priority (>= -10 seems to be enough) such that your controller action has actually returned the view model before testing the condition.