3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Controller\Plugin
;
12 use Zend\Mvc\Exception
;
13 use Zend\Mvc\InjectApplicationEventInterface
;
14 use Zend\Mvc\MvcEvent
;
15 use Zend\View\Model\ModelInterface
as Model
;
17 class Layout
extends AbstractPlugin
25 * Set the layout template
27 * @param string $template
30 public function setTemplate($template)
32 $viewModel = $this->getViewModel();
33 $viewModel->setTemplate((string) $template);
40 * If no arguments are given, grabs the "root" or "layout" view model.
41 * Otherwise, attempts to set the template for that view model.
43 * @param null|string $template
44 * @return Model|Layout
46 public function __invoke($template = null)
48 if (null === $template) {
49 return $this->getViewModel();
51 return $this->setTemplate($template);
58 * @throws Exception\DomainException if unable to find event
60 protected function getEvent()
66 $controller = $this->getController();
67 if (!$controller instanceof InjectApplicationEventInterface
) {
68 throw new Exception\
DomainException('Layout plugin requires a controller that implements InjectApplicationEventInterface');
71 $event = $controller->getEvent();
72 if (!$event instanceof MvcEvent
) {
73 $params = $event->getParams();
74 $event = new MvcEvent();
75 $event->setParams($params);
77 $this->event
= $event;
83 * Retrieve the root view model from the event
86 * @throws Exception\DomainException
88 protected function getViewModel()
90 $event = $this->getEvent();
91 $viewModel = $event->getViewModel();
92 if (!$viewModel instanceof Model
) {
93 throw new Exception\
DomainException('Layout plugin requires that event view model is populated');