From 2482660f3702b1da96c8506ed1ead92a15577ab6 Mon Sep 17 00:00:00 2001 From: Jessie Green Date: Fri, 21 Mar 2014 21:46:03 -0500 Subject: [PATCH] Add the ability to add a custom layout for a not found event using the "NotFound" key. --- Module.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Module.php b/Module.php index 019a842..84bb766 100644 --- a/Module.php +++ b/Module.php @@ -1,15 +1,32 @@ getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', function($e) { + $config = $e->getApplication()->getServiceManager()->get('config'); + $eventManager = $e->getApplication()->getEventManager(); + + $eventManager->attach( + MvcEvent::EVENT_DISPATCH_ERROR, + function(\Zend\Mvc\MvcEvent $e) use($config) { + if($e->getError() === Application::ERROR_ROUTER_NO_MATCH) { + if(isset($config['module_layouts']['NotFound'])){ + $e->getViewModel()->setTemplate($config['module_layouts']['NotFound']); + } + } + }, + 100 + ); + + $eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', function($e) use($config) { $controller = $e->getTarget(); $controllerClass = get_class($controller); $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\')); - $config = $e->getApplication()->getServiceManager()->get('config'); + if (isset($config['module_layouts'][$moduleNamespace])) { $controller->layout($config['module_layouts'][$moduleNamespace]); }