Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit dc0efa3

Browse files
committed
Adds cookbook recipe for setting module-specific layout
1 parent 8bad7f9 commit dc0efa3

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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).

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pages:
3030
- Placeholder: helpers/placeholder.md
3131
- Url: helpers/url.md
3232
- "Advanced usage of helpers": helpers/advanced-usage.md
33+
- Cookbook:
34+
- "Setting module-specific Layout": cookbook\setting-module-specific-layout.md
3335
site_name: zend-view
3436
site_description: zend-view
3537
repo_url: 'https://github.com/zendframework/zend-view'

0 commit comments

Comments
 (0)