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]); }