PHP Zend Framework 2 - Renderer 正在意外文件夹中查找模板


PHP Zend Framework 2 - Renderer is looking for the template in an unexpected folder

我在ZF2安装中创建了一个名为AB2CD的新模块。IndexController 的 indexAction 只返回一个新的 ViewModel()。我收到的错误消息是:

Zend'View'Renderer'PhpRenderer::render: Unable to render template "ab2-cd/index/index"; resolver could not resolve to a file

我在module.config.php中的视图管理器是:

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

它会在哪里提出文件夹ab2-cd?我可以通过重命名文件夹来解决错误,但我希望了解为什么它一开始找错了地方......

ab2-cd是从模块名称AB2CD生成的。 如果您不这样做,则自动生成的名称可以使用setTemplate方法手动提供视图模型的模板名称。

如果你想继续使用自动生成的名称,你的目录结构应该是这样的:

--module
 |--AB2CD
   |--view
     |--ab2-cd
       |--[lower-cased-controller-name]
         |--[lower-cased-action-name]

除了Exlord的答案之外,您不必在控制器中手动设置模板,您还可以在module.config中提供模板映射.php

'view_manager' => array(
   ...
   'template_map' => array(
      'ab2-cd/index/index' => __DIR__ . '/../view/AB2CD/index/index.phtml',
   ),
   ...
),

我还建议不要使用带有数字的模块名称。像AbToCd这样可以转换为ab-to-cd/index/index的东西在psr-0上会更好地工作。