Zend Framework 2 change layout for ajax requests

September 15, 2015

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',
        ),
    ),

tags: ,
posted in Zend Framework 2 by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /home/c2kblate/sites/ivangospodinow.com/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org