|
| 1 | +# Setting module-specific Layouts |
| 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 | +listens 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 controller from the current request. |
| 10 | + |
| 11 | +## Create Listener |
| 12 | + |
| 13 | +Create a listener as a 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\Filter\FilterChain; |
| 22 | +use Zend\Filter\FilterInterface; |
| 23 | +use Zend\Filter\StringToLower; |
| 24 | +use Zend\Filter\Word\CamelCaseToDash; |
| 25 | +use Zend\Mvc\MvcEvent; |
| 26 | +use Zend\View\Resolver\TemplateMapResolver; |
| 27 | + |
| 28 | +class LayoutListener extends AbstractListenerAggregate |
| 29 | +{ |
| 30 | + /** @var TemplateMapResolver */ |
| 31 | + private $templateMapResolver; |
| 32 | + |
| 33 | + /** @var FilterInterface */ |
| 34 | + private $filter; |
| 35 | + |
| 36 | + public function __construct(TemplateMapResolver $templateMapResolver) |
| 37 | + { |
| 38 | + $this->templateMapResolver = $templateMapResolver; |
| 39 | + $this->filter = (new FilterChain()) |
| 40 | + ->attach(new CamelCaseToDash()) |
| 41 | + ->attach(new StringToLower()); |
| 42 | + } |
| 43 | + |
| 44 | + public function attach(EventManagerInterface $events, $priority = 1) |
| 45 | + { |
| 46 | + $this->listeners[] = $events->attach( |
| 47 | + MvcEvent::EVENT_RENDER, |
| 48 | + [$this, 'setLayout'] |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + public function setLayout(MvcEvent $event) : void |
| 53 | + { |
| 54 | + // Get and check the route match object |
| 55 | + $routeMatch = $event->getRouteMatch(); |
| 56 | + if (! $routeMatch) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Get and check the parameter for current controller |
| 61 | + $controller = $routeMatch->getParam('controller'); |
| 62 | + if (! $controller) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Extract module name |
| 67 | + $module = substr($controller, 0, strpos($controller, '\\')); |
| 68 | + |
| 69 | + // Convert the module name from camel case to a lower string with dashes |
| 70 | + $name = 'layout/' . $this->filter->filter($module); |
| 71 | + |
| 72 | + // Has the resolver an entry / layout with the given name? |
| 73 | + if (! $this->templateMapResolver->has($name)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Get root view model |
| 78 | + $layoutViewModel = $event->getViewModel(); |
| 79 | + |
| 80 | + // Change template |
| 81 | + $layoutViewModel->setTemplate($name); |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Register Listener |
| 87 | + |
| 88 | +Extend the module class to register the listener, e.g. |
| 89 | +`module/Admin/Module.php`: |
| 90 | + |
| 91 | +```php |
| 92 | +namespace Admin; |
| 93 | + |
| 94 | +use Admin\Listener\LayoutListener; |
| 95 | +use Zend\Mvc\MvcEvent; |
| 96 | +use Zend\View\Resolver\TemplateMapResolver; |
| 97 | + |
| 98 | +class Module |
| 99 | +{ |
| 100 | + public function onBootstrap(MvcEvent $event) : void |
| 101 | + { |
| 102 | + $application = $event->getApplication(); |
| 103 | + |
| 104 | + /** @var TemplateMapResolver $templateMapResolver */ |
| 105 | + $templateMapResolver = $application->getServiceManager()->get( |
| 106 | + 'ViewTemplateMapResolver' |
| 107 | + ); |
| 108 | + |
| 109 | + // Create and register layout listener |
| 110 | + $listener = new LayoutListener($templateMapResolver); |
| 111 | + $listener->attach($application->getEventManager()); |
| 112 | + } |
| 113 | + |
| 114 | + // … |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +> More informations on registering module-specific listeners can be found in the |
| 119 | +> [documentation of zend-mvc](https://docs.zendframework.com/zend-mvc/examples/#registering-module-specific-listeners). |
| 120 | +
|
| 121 | +## Add Template Scripts to the Configuration |
| 122 | + |
| 123 | +Extend the configuration of a module to add the specific layout script, e.g. |
| 124 | +`module/Admin/config/module.config.php`: |
| 125 | + |
| 126 | +```php |
| 127 | +return [ |
| 128 | + // Add the following array |
| 129 | + 'view_manager' => [ |
| 130 | + 'template_map' => [ |
| 131 | + 'layout/admin' => __DIR__ . '/../view/layout/admin.phtml', |
| 132 | + ], |
| 133 | + ], |
| 134 | + // … |
| 135 | +]; |
| 136 | +``` |
| 137 | + |
| 138 | +And in another module, e.g. `module/Album/config/module.config.php`: |
| 139 | + |
| 140 | +```php |
| 141 | +return [ |
| 142 | + // Add the following array |
| 143 | + 'view_manager' => [ |
| 144 | + 'template_map' => [ |
| 145 | + 'layout/album' => __DIR__ . '/../view/layout/layout-of-album.phtml', |
| 146 | + ], |
| 147 | + ], |
| 148 | + // … |
| 149 | +]; |
| 150 | +``` |
| 151 | + |
| 152 | +The name of the array key must follow the format `layout/{module-name}` and the |
| 153 | +value must contain the path to the layout file. The path and the filename can be |
| 154 | +freely chosen. |
0 commit comments