Created
March 16, 2018 14:41
-
-
Save alexdenvir/1b63a5d3acc7a6678e1fb994db0617a9 to your computer and use it in GitHub Desktop.
Custom InjectTemplateListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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