Zend框架2错误:无法呈现模板

时间:2012-12-06 15:15:03

标签: zend-framework2

我正在研究this tutorial。我已按照所有步骤创建目录结构,控制器,module.php和module.config.php,但是当我打开http://zf2-tutorial/album时出现以下错误:

 Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file' in /var/www/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:461

我在view / album / album目录中的模块目录中创建了一个名为index.phtml的文件。

module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController',       
        ),
        'view_manager' => array(
            'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'album/album/index' => __DIR__ . '/../view/album/album/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
                ),
            'template_path_stack' => array(
                'album' => __DIR__ . '/../view',
            )
        )
    ),

    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

);

怎么了?

12 个答案:

答案 0 :(得分:6)

PhpRenderer对象抱怨,因为它无法找到视图phtml文件。

文件module/Album/view/album/album/index.phtml是否存在?如果没有,则可能是原因。

答案 1 :(得分:3)

检查您是否在 module.config.php 文件中为 view_manager 提供了正确的参数或嵌套

答案 2 :(得分:2)

我遇到了同样的问题。我已经改变了文件module.config.php的下面一行, 存在于文档中,

'view_manager' => array(
  'template_path_stack' => array(
  'album' => __DIR__ . '/../view',
  ),
),

'view_manager' => array(
  'template_path_stack' => array(
  'album' => __DIR__ . '/../src/view',
  ),
),

只需在视图文件夹前面添加src文件夹名称。

像这样,您需要在所有视图路径之前添加'src'文件夹名称。

答案 3 :(得分:1)

您应该在view中返回IndexController对象

$view = new ViewModel(array('albums' => $this->getAlbumTable()->fetchAll(),));
$view->setTemplate("album/album/index/index.phtml");
return $view;

而不是直接返回

return array('albums' => $this->getAlbumTable()->fetchAll(),);

答案 4 :(得分:0)

我已使用相册模块检查了您的代码。一切都好。你可能有htaccess文件的问题。请检查。

答案 5 :(得分:0)

您的view_manager位置错误。它应该在“控制器”和“路由器”级别,而不是在“控制器”内。查看Application模块中的module.config.php并查看差异。

答案 6 :(得分:0)

确保在 module.map 数组中使用正斜杠而不是反斜杠,该数组位于 module.config.php view_manager

答案 7 :(得分:0)

除此之外,你需要在module.config.php中添加两件事 在路由数组中放置代码

'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
        ),

和翻译器数组以及语言文件夹

'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

答案 8 :(得分:0)

Album \ config \ module.config.php中不再有“home”路由 所以

有必要在.. \ module \ Album \ view \ layout \ layout.phtml中将'H / home'替换为'A / album'

答案 9 :(得分:0)

就我而言,问题出在视图文件名中。我将其命名为index.php而不是index.phtml

答案 10 :(得分:0)

出现同样的问题,我没有创建空视图文件     模块/专辑/视图/专辑/专辑/ index.phtml     模块/专辑/视图/专辑/专辑/ add.phtml     模块/专辑/视图/专辑/专辑/ edit.phtml     模块/专辑/视图/专辑/专辑/ delete.phtml

答案 11 :(得分:0)

检查你的application.config.php模块名称,如果它是'Album'而不是'album' 改变

  country income
  --------------
  es    500
  gb    200
  sg    300
  id    500
  de    450