How to change layout for ajax requests in Zend Framework 2
1. Module.php
public function onBootstrap(MvcEvent $e) { $e->getApplication() ->getEventManager() ->getSharedManager() ->attach( 'Zend\Mvc\Controller\AbstractController', 'dispatch', function ($e) { $controller = $e->getTarget(); if ($controller->getRequest() instanceof HttpRequest) { if ($controller->getRequest()->isXmlHttpRequest()) { $controller->layout('layout/ajax'); } } }, 100 ); }
2. module.config.php Add the layout path
'view_manager' => array( 'template_map' => array( 'layout/ajax' => __DIR__ . '/../view/layout/layout.ajax.phtml', ), ),