企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
为了使所有的模板文件能够看起来足够简洁和便于理解,在模板文件的编写上将尽量的用少的代码及多的常用代码来进行讲解,而且在模板文件中较多的代码仍为普通的HTML代码;所以在下面涉及到的 css 文件和 js 文件都是以空文件的形式来引用,使用空文件的原因主要是为说明相关函数的功能特性,同时对模板文件进行解释的主要内容也在ZF2的函数上。 ### 5.3.1 模板文件layout.phtml 下面是代码内容: ~~~ <?php echo $this->doctype(); ?> <html> <head> <meta charset="utf-8"> <?php echo $this->headTitle($this->translate('doc title')); ?> <?php echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0'); ?> <?php echo $this->headLink()->prependStylesheet($this->basePath() . '/css/style.css'); ?> <?php echo $this->headScript()->prependFile($this->basePath() . '/js/jquery.min.js'', 'text/javascript');?> </head> <body> <table border="1" cellspacing="0" cellspadding="0" width="100%"> <tr> <td>header</td> </tr> <tr><td><?php echo $this->content; ?></td></tr> <tr><td>footer</td></tr> </table> </body> </html> ~~~ 模板内容解释: echo $this->doctype() 文档使用的类型,这个类型与在模块配置文件中设置的类型有关 echo $this->headTitle(); 输出文档标题 $this->translate('doc title') 转换文档标题,此函数功能的实现需要语言包的支持 echo $this->headMeta() 输出HTML人 Meta 属性 appendName('viewport', 'width=device-width, initial-scale=1.0') 设置Meta 属性的具体内容 echo $this->headLink() 输出link标签 prependStylesheet($this->basePath() . '/css/style.css') 设置link标签属性 $this->basePath() 获取站点根路径 echo $this->headScript() 输出 script 标签 prependFile($this->basePath() . '/js/jquery.min.js'', 'text/javascript') 设置 script 标签属性 echo $this->content 输出控制器对应的模板页面内容 以上的内容就是一个简单的layout 而已模板,没有复杂的代码,没有复杂的样式;布局的结构最后将呈现出 上-中-下 的三行结构;上部放置导航内容,中部放置页面主要内容,下部放置版权信息等。所以最终看到的界面大概如下所示: header 头部内容 content 正文内容 footer 底部内容 ### 5.3.2 错误异常模板 index.phtml 下面是代码内容: ~~~ <h1><?php echo $this->translate('An error occurred') ?></h1> <h2><?php echo $this->message ?></h2> <?php if (isset($this->display_exceptions) && $this->display_exceptions): ?> <?php if(isset($this->exception) && $this->exception instanceof Exception): ?> <hr/> <h2><?php echo $this->translate('Additional information') ?>:</h2> <h3><?php echo get_class($this->exception); ?></h3> <dl> <dt><?php echo $this->translate('File') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $this->exception->getFile() ?>:<?php echo $this->exception->getLine() ?></pre> </dd> <dt><?php echo $this->translate('Message') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $this->exception->getMessage() ?></pre> </dd> <dt><?php echo $this->translate('Stack trace') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $this->exception->getTraceAsString() ?></pre> </dd> </dl> <?php $e = $this->exception->getPrevious(); if ($e) : ?> <hr/> <h2><?php echo $this->translate('Previous exceptions') ?>:</h2> <ul> <?php while($e) : ?> <li> <h3><?php echo get_class($e); ?></h3> <dl> <dt><?php echo $this->translate('File') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $e->getFile() ?>:<?php echo $e->getLine() ?></pre> </dd> <dt><?php echo $this->translate('Message') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $e->getMessage() ?></pre> </dd> <dt><?php echo $this->translate('Stack trace') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $e->getTraceAsString() ?></pre> </dd> </dl> </li> <?php $e = $e->getPrevious(); endwhile; ?> </ul> <?php endif; ?> <?php else: ?> <h3><?php echo $this->translate('No Exception available') ?></h3> <?php endif ?> <?php endif ?> ~~~ 代码解释: echo $this->message 输出错误信息 if (isset($this->display_exceptions) && $this->display_exceptions) 判断是否显示异常信息 echo get_class($this->exception) 输出异常类型名称 echo $this->exception->getFile() 输出导致异常的文件名 echo $this->exception->getLine() 输出导致异常文件的所在行 echo $this->exception->getMessage() 输出异常信息 echo $this->exception->getTraceAsString() 输出异常堆栈信息 $e = $this->exception->getPrevious() 获取上一个异常 以上是错误异常模板内容,模板能够输出导致错误异常的文件名、出错所在行、错误类型等信息。在开发项目的时候便可以通过错误的信息提示来查找相关出错原因。理解并正确使用使用错误信息能够有效的提高开发效率。 ### 5.3.3 404错误模板 404.phtml 下面是代码内容: ~~~ <h1><?php echo $this->translate('A 404 error occurred') ?></h1> <h2><?php echo $this->message ?></h2> <?php if (isset($this->reason) && $this->reason): ?> <?php $reasonMessage= ''; switch ($this->reason) { case 'error-controller-cannot-dispatch': $reasonMessage = $this->translate('The requested controller was unable to dispatch the request.'); break; case 'error-controller-not-found': $reasonMessage = $this->translate('The requested controller could not be mapped to an existing controller class.'); break; case 'error-controller-invalid': $reasonMessage = $this->translate('The requested controller was not dispatchable.'); break; case 'error-router-no-match': $reasonMessage = $this->translate('The requested URL could not be matched by routing.'); break; default: $reasonMessage = $this->translate('We cannot determine at this time why a 404 was generated.'); break; } ?> <p><?php echo $reasonMessage ?></p> <?php endif ?> <?php if (isset($this->controller) && $this->controller): ?> <dl> <dt><?php echo $this->translate('Controller') ?>:</dt> <dd><?php echo $this->escapeHtml($this->controller) ?> <?php if (isset($this->controller_class) && $this->controller_class && $this->controller_class != $this->controller ) { echo '(' . sprintf($this->translate('resolves to %s'), $this->escapeHtml($this->controller_class)) . ')'; } ?> </dd> </dl> <?php endif ?> <?php if (isset($this->display_exceptions) && $this->display_exceptions): ?> <?php if(isset($this->exception) && $this->exception instanceof Exception): ?> <hr/> <h2><?php echo $this->translate('Additional information') ?>:</h2> <h3><?php echo get_class($this->exception); ?></h3> <dl> <dt><?php echo $this->translate('File') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $this->exception->getFile() ?>:<?php echo $this->exception->getLine() ?></pre> </dd> <dt><?php echo $this->translate('Message') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $this->exception->getMessage() ?></pre> </dd> <dt><?php echo $this->translate('Stack trace') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $this->exception->getTraceAsString() ?></pre> </dd> </dl> <?php $e = $this->exception->getPrevious(); if ($e) : ?> <hr/> <h2><?php echo $this->translate('Previous exceptions') ?>:</h2> <ul> <?php while($e) : ?> <li> <h3><?php echo get_class($e); ?></h3> <dl> <dt><?php echo $this->translate('File') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $e->getFile() ?>:<?php echo $e->getLine() ?></pre> </dd> <dt><?php echo $this->translate('Message') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $e->getMessage() ?></pre> </dd> <dt><?php echo $this->translate('Stack trace') ?>:</dt> <dd> <pre class="prettyprint linenums"><?php echo $e->getTraceAsString() ?></pre> </dd> </dl> </li> <?php $e = $e->getPrevious(); endwhile; ?> </ul> <?php endif; ?> <?php else: ?> <h3><?php echo $this->translate('No Exception available') ?></h3> <?php endif ?> <?php endif ?> ~~~ 代码解析: echo $this->message 输出错误信息 if (isset($this->reason) && $this->reason) 判断是否存在错误,$this->reason 有多种类型,控制器路由分发错误(error-controller-cannot-dispatch)、控制器不存在(error-controller-not-found)、无效控制器(error-controller-invalid)、路由不匹配(error-router-no-match)及其他 $this->controller 表示控制器名 $this->controller_class 表示控制器类名 以上内容是404错误提示模板内容;模板能够输出导致错误异常的文件名、出错所在行、错误类型等信息。在后继开发中可以通过404的相关提示信息找到出错的问题点。