ZendFramework2入门教程第5章 创建视图模板5.2 模板配置

5.2 模板配置

建立好布局文件、模板文件之后需要对他们进行配置,以便后续使用中能够让ZF2自动的搜索并加载模板文件。其实对模板的配置在前面的章节已经有一些讲解,因为模板的配置也是一个重点的内容,在此对模板的配置再详细的解释一遍。打开文件:/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',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            'application' => __DIR__ . '/../view'
        ),
),

view_manager 节点与 router、controllers 节点属于同一级。

  • view_manager 表示此数组块为视图管理器配置信息段
  • view_manager-->display_not_found_reason 是否显示404原因
  • view_manager-->display_exceptions 是否显示异常信息
  • view_manager-->doctype 指定视图页面类型
  • view_manager-->not_found_template 指定404的视图模板
  • view_manager-->exception_template 指定异常模板文件
  • view_manager-->template_map 视图模块地图
  • view_manager-->template_map-->’layout/layout’ 指定布局文件
  • view_manager-->template_map-->’error/404’ 指定404页面的视图文件
  • view_manager-->template_map-->’error/index’ 指定错误异常页面的视图文件
  • view_manager-->template_path_stack 视图模板堆栈路径
  • view_manager-->template_path_stack-->application 指定模块application 视图目录所在路径

经过以上 建立目录、文件、配置后 NewsController 就是可以直接自动的搜索对应的布局和模板等资源文件。由于以上都只是建立了空文件,在实际应用中没有什么意义,所以接下将对布局、错误异常的模板添加一些代码,致以news 目录里的模板文件的内容将在后续的讲解中逐一的进行添加填充。