4

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

“主”模块的配置如下所示:

<?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: Unable to render template "layout/layout"; 解析器无法解析到 D:\WebServer\www\homepage\vendor\library\Zend\View\Renderer\PhpRenderer.php 中第 461 行 (!) Zend\View\Exception\RuntimeException: Zend\View\Renderer 中的文件\PhpRenderer::render: 无法渲染模板“布局/布局”;解析器无法解析到第 461 行 D:\WebServer\www\homepage\vendor\library\Zend\View\Renderer\PhpRenderer.php 中的文件

什么原因?

4

3 回答 3

5

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

于 2013-05-20T17:25:49.740 回答
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',
),
于 2014-06-04T13:28:25.777 回答
-1

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

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

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

你能试试吗?

于 2013-03-07T14:46:07.020 回答