Zend Framework 2无法呈现视图,解析程序无法解析为文件.为什么?


Zend Framework 2 fails to render view, resolver could not resolve to a file. Why?

出于学习目的,我试图在Zend Framework 2中从头开始创建一个模块,但我无法让它呈现视图。它总是抛出这样的错误:

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

我理解错误所说的:与请求相对应的视图文件丢失,但我不明白为什么会发生这种情况——对我来说,一切都已就绪。也许我只是忽略了一些东西,但我似乎找不到

我的module.config.php看起来像这样:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'MyModule'Controller'IndexController' => 'MyModule'Controller'IndexController'
        ),
    ),
    'router' => array(
        'routes' => array(
            'my-module' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/my-module',
                    'defaults' => array(
                        'controller' => 'MyModule'Controller'IndexController',
                        'action' => 'index',
                    ),
                )
            ),
        ),
        'view_manager' => array(
            'template_path_stack' => array(
                __DIR__ . '/../view',
            ),
        ),
    ),
);

我的视图位于module/MyModule/view/my-module/index/index.phtml

我也尝试过module/MyModule/view/my-module/index/index/index.phtml,但这在我看来是错误的,也不起作用——为什么会出现预期的视图?我的配置或文件/文件夹结构哪里错了?为什么框架找不到正确的视图文件?

也许还可以看看控制器:

namespace MyModule'Controller;
use Zend'Mvc'Controller'AbstractActionController;
use Zend'View'Model'ViewModel;
class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

您的view_manager配置位于错误的位置,您已将其放入router配置中,这意味着您的模板文件夹从未添加到堆栈中。移动密钥。。。

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'MyModule'Controller'IndexController' => 'MyModule'Controller'IndexController'
        ),
    ),
    'router' => array(
        'routes' => array(
            'my-module' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/my-module',
                    'defaults' => array(
                        'controller' => 'MyModule'Controller'IndexController',
                        'action' => 'index',
                    ),
                )
            ),
        ),
        // view_manager config doesn't belong here
    ),
    // correct place for view_manager config is here
    'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);