Skip to content

Instantly share code, notes, and snippets.

@alexdenvir
Created March 16, 2018 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdenvir/1b63a5d3acc7a6678e1fb994db0617a9 to your computer and use it in GitHub Desktop.
Save alexdenvir/1b63a5d3acc7a6678e1fb994db0617a9 to your computer and use it in GitHub Desktop.
Custom InjectTemplateListener
<?php
declare(strict_types=1);
namespace Application\Mvc\View\Http;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\ViewModel;
class InjectTemplateListener extends AbstractListenerAggregate
{
public function attach(EventManagerInterface $event_manager, $priority = 1)
{
$this->listeners[] = $event_manager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'injectTemplate'], -91);
}
public function injectTemplate(MvcEvent $event)
{
$model = $event->getResult();
if (!$model instanceof ViewModel) {
return;
}
$controller = $event->getTarget();
if (is_object($controller) && $controller instanceof TemplateNameProviderInterface) {
$model->setTemplate($controller->getTemplateName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment