|
| 1 | +# Setting module-specific Layout |
| 2 | + |
| 3 | +The following example shows how to set a template for the layout based on a |
| 4 | +module name in a zend-mvc based application. The example uses a listener that |
| 5 | +listen on the |
| 6 | +[`Zend\Mvc\MvcEvent::EVENT_RENDER` event](https://docs.zendframework.com/zend-mvc/mvc-event/#mvceventevent_render-render) |
| 7 | +and uses the |
| 8 | +[`Zend\Router\RouteMatch` object](https://docs.zendframework.com/zend-mvc/routing/#routing) |
| 9 | +to get the called module from current request. |
| 10 | + |
| 11 | +## Create Listener |
| 12 | + |
| 13 | +Create a listener as separate class, e.g. |
| 14 | +`module/Admin/src/Listener/LayoutListener.php`: |
| 15 | + |
| 16 | +```php |
| 17 | +namespace Admin\Listener; |
| 18 | + |
| 19 | +use Zend\EventManager\AbstractListenerAggregate; |
| 20 | +use Zend\EventManager\EventManagerInterface; |
| 21 | +use Zend\Mvc\MvcEvent; |
| 22 | + |
| 23 | +class LayoutListener extends AbstractListenerAggregate |
| 24 | +{ |
| 25 | + public function attach(EventManagerInterface $events, $priority = 1) |
| 26 | + { |
| 27 | + $this->listeners[] = $events->attach( |
| 28 | + MvcEvent::EVENT_RENDER, |
| 29 | + [ |
| 30 | + $this, |
| 31 | + 'setLayout', |
| 32 | + ] |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function setLayout(MvcEvent $event) : void |
| 37 | + { |
| 38 | + // Get route match object |
| 39 | + $routeMatch = $event->getRouteMatch(); |
| 40 | + |
| 41 | + // Check route match and parameter for current module |
| 42 | + if ($routeMatch |
| 43 | + && $routeMatch->getParam('module') === 'Admin' |
| 44 | + ) { |
| 45 | + // Get root view model |
| 46 | + $layoutViewModel = $event->getViewModel(); |
| 47 | + |
| 48 | + // Change template |
| 49 | + $layoutViewModel->setTemplate('layout/backend'); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Register Listener |
| 56 | + |
| 57 | +Extend the module class to register the listener, e.g. |
| 58 | +`module/Admin/Module.php`: |
| 59 | + |
| 60 | +```php |
| 61 | +namespace Admin; |
| 62 | + |
| 63 | +use Application\Listener\LayoutListener; |
| 64 | +use Zend\EventManager\EventInterface; |
| 65 | +use Zend\ModuleManager\Feature\BootstrapListenerInterface; |
| 66 | + |
| 67 | +class Module implements BootstrapListenerInterface |
| 68 | +{ |
| 69 | + public function onBootstrap(EventInterface $e) |
| 70 | + { |
| 71 | + /** @var \Zend\Mvc\MvcEvent $e */ |
| 72 | + |
| 73 | + $application = $e->getApplication(); |
| 74 | + |
| 75 | + // Create and register layout listener |
| 76 | + $layoutAggregate = new LayoutListener(); |
| 77 | + $layoutAggregate->attach($application->getEventManager()); |
| 78 | + } |
| 79 | + |
| 80 | + // … |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +> More informations on registering module-specific listeners can be found in the |
| 85 | +> [documentation of zend-mvc](https://docs.zendframework.com/zend-mvc/examples/#registering-module-specific-listeners). |
0 commit comments