在与ZF2一起使用的基本示例应用程序中,IndexController的编码如下:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
}
只有一个简单的return语句,index.phtml
的内容会自动包含在输出中。
我看到引用index.phtml
的唯一地方是\module\Application\config\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',
),
),
我试过评论这一行:
//'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
但是index.phtml仍然包含在输出中。
所以问题是,它为什么包括在内?幕后发生什么魔法导致它仍被包括在内?
在我看来,也许我错过了ZF2处理的一些我在文档中无法找到的基本自动映射。但如果有一些自动映射,为什么module.config.php
?
答案 0 :(得分:1)
所以事实证明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',
),
),
这一行是静态映射:
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
这些行是动态映射:
'template_path_stack' => array(
__DIR__ . '/../view',
),
此页面上的用法部分向我说明了这一点。 http://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html
它在可以提高性能时使用静态映射,然后故障转移到动态映射。注释掉两者实际上都会导致错误,ViewModel()无法找到要包含的页面。
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/index/index"; resolver could not resolve to a file'