ZF2 EdpModuleLayouts

I have a problem with the EdpModuleLayouts module. I placed Module.php in the module / EdpModuleLayouts / directory with the following contents:

<?php namespace EdpModuleLayouts; class Module { public function onBootstrap($e) { $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) { $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]); } }, 100); } } 

I also registered it in the config / application.config.php file:

 return array( 'modules' => array( 'EdpModuleLayouts', 'Main', 'Administrator', 'Object' ), 'module_layouts' => array( 'Main' => 'layout/main', 'Administrator' => 'layout/admin', ), 'module_listener_options' => array( 'module_paths' => array( './module', ), 'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ), ), ); 

The configuration of the "main" module looks like this:

 <?php return array( 'router' => array( 'routes' => array( 'Main' => array( 'type' => 'Literal', 'options' => array( 'route' => '/main', 'defaults' => array( '__NAMESPACE__' => 'Main\Controller', 'controller' => 'Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '[/:controller][/:action][/:id]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 'id' => '[0-9]*', ), 'defaults' => array( ), ), ), ), ), ), ), 'service_manager' => array( 'factories' => array(), ), 'controllers' => array( 'invokables' => array( 'Main\Controller\Index' => 'Main\Controller\EmailController', 'Main\Controller\Error' => 'Main\Controller\ErrorController', 'Main\Controller\FAQ' => 'Main\Controller\FAQController', 'Main\Controller\Index' => 'Main\Controller\IndexController', 'Main\Controller\Pages' => 'Main\Controller\PagesController', 'Main\Controller\Settings' => 'Main\Controller\SettingsController', 'Main\Controller\User' => 'Main\Controller\UserController', ), ), 'view_manager' => array( 'template_map' => array( 'layout/main' => __DIR__ . '/../view/layout/main_layout.phtml', 'layout/header' => __DIR__ . '/../view/layout/main_header.phtml', 'layout/footer' => __DIR__ . '/../view/layout/main_footer.phtml', 'index/index' => __DIR__ . '/../view/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', ), 'display_exceptions' => true, 'exception_template' => 'error/index', 'display_not_found_reason' => true, 'not_found_template' => 'error/404', ), ); 

But when I access any module in the application that I want, it throws an exception:

(!) Fatal error: exception "Zend \ View \ Exception \ RuntimeException" with the message "Zend \ View \ Renderer \ PhpRenderer :: render: cannot display the template" layout / layout "; resolver cannot resolve the file 'in D: \ WebServer \ www \ homepage \ vendor \ library \ Zend \ View \ Renderer \ PhpRenderer.php on line 461 (!) Zend \ View \ Exception \ RuntimeException: Zend \ View \ Renderer \ PhpRenderer :: render: cannot display the "layout / layout "; resolver could not resolve the file in D: \ WebServer \ www \ homepage \ vendor \ library \ Zend \ View \ Renderer \ PhpRenderer.php on line 461

What reason?

+4
source share
3 answers

As a newbie to ZF2, it took me a while for the EdpModuleLayouts module to work. The key is to configure the "view_manager" and "template_map" parameters. Please visit http://www.webtrafficexchange.com/zf2-configure-layout-each-module-edpmodulelayouts for sample configurations.

+5
source

All this is done by installing the appropriate layout.

 controller->layout($config['module_layouts'][$moduleNamespace]); 

The error message says that he could not find the template that he is trying to install. You still need to customize your layouts on the template map. Here is an example from one of my projects.

 'view_manager' => array( 'template_map' => array( 'cms-admin/admin/dashboard' => __DIR__ . '/../view/cms-admin/admin/dashboard.phtml', 'cms-admin/login/login' => __DIR__ . '/../view/cms-admin/login/login.phtml', 'cms-admin/users/index' => __DIR__ . '/../view/cms-admin/admin/users/index.phtml' ) ), 'module_layouts' => array( 'CmsAdmin' => 'layout/admin-layout', ), 
+1
source

Is if (isset ($ config ['module_layouts'] [$ moduleNamespace])) set to true? I think your module_layouts variant should be in the configuration directory:

./configuration/layouts.config.glob.php:

 <?php return array( 'module_layouts' => array( 'Main' => 'layout/main', 'Administrator' => 'layout/admin', ), ); ?> 

Could you try a try?

-1
source

All Articles