ZF2 EdpModuleLayouts

时间:2013-03-05 12:55:06

标签: layout module zend-framework2

我的EdpModuleLayouts模块有问题。我将Module.php放在module / EdpModuleLayouts /目录中,其中包含以下内容:

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

我也在config / application.config.php中注册了它:

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',
    ),
),
);

“main”模块的配置如下:

<?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',
),
);

但是当我访问我想要的应用程序中的任何模块时,它会引发异常:

(!)致命错误:未捕获的异常'Zend \ View \ Exception \ RuntimeException',消息'Zend \ View \ Renderer \ PhpRenderer :: render:无法渲染模板“layout / layout”;解析器无法解析为第461行的D:\ WebServer \ www \ homepage \ vendor \ library \ Zend \ View \ Renderer \ PhpRenderer.php中的文件 (!)Zend \ View \ Exception \ RuntimeException:Zend \ View \ Renderer \ PhpRenderer :: render:无法渲染模板“layout / layout”;解析器无法解析到第461行的D:\ WebServer \ www \ homepage \ vendor \ library \ Zend \ View \ Renderer \ PhpRenderer.php中的文件

是什么原因?

3 个答案:

答案 0 :(得分:5)

作为ZF2的新手,我花了一些时间让EdpModuleLayouts模块工作。关键是配置“view_manager”和“template_map”设置。有关配置示例,请访问http://www.webtrafficexchange.com/zf2-configure-layout-each-module-edpmodulelayouts

答案 1 :(得分:1)

所有这一切都是设置适当的布局。

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

错误消息告诉您它无法找到它尝试设置的模板。您仍需要在模板映射中配置布局。以下是我的一个项目的示例。

'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',
),

答案 2 :(得分:-1)

if(isset($ config ['module_layouts'] [$ moduleNamespace]))是否计算为true?我认为你的选项module_layouts应该在config目录中:

./配置/ layouts.config.glob.php:

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

你可以尝试一下吗?