2015-04-03 115 views
0

为了学习的目的,我试图从头开始在Zend Framework 2中创建一个模块,但我无法让它呈现视图。它总是抛出这个错误: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(); 
    } 
} 

回答

1

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

当!我只是没有看到,谢谢! – Sven 2015-04-03 12:19:38

相关问题