2015-04-03 117 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

相關問題