5.3 编写布局和错误异常模板
为了使所有的模板文件能够看起来足够简洁和便于理解,在模板文件的编写上将尽量的用少的代码及多的常用代码来进行讲解,而且在模板文件中较多的代码仍为普通的HTML代码;所以在下面涉及到的 css 文件和 js 文件都是以空文件的形式来引用,使用空文件的原因主要是为说明相关函数的功能特性,同时对模板文件进行解释的主要内容也在ZF2的函数上。
5.3.1 模板文件layout.phtml
下面是代码内容:
doctype(); ?>
headTitle($this->translate('doc title')); ?>
headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0'); ?>
headLink()->prependStylesheet($this->basePath() . '/css/style.css'); ?>
headScript()->prependFile($this->basePath() . '/js/jquery.min.js'', 'text/javascript');?>
header
content; ?>
footer
模板内容解释:
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
下面是代码内容:
translate('An error occurred') ?>
message ?>
display_exceptions) && $this->display_exceptions): ?>
exception) && $this->exception instanceof Exception): ?>
translate('Additional information') ?>:
exception); ?>
-
translate('File') ?>:
-
exception->getFile() ?>:
exception->getLine() ?>
-
translate('Message') ?>:
-
exception->getMessage() ?>
-
translate('Stack trace') ?>:
-
exception->getTraceAsString() ?>
exception->getPrevious();
if ($e) :
?>
translate('Previous exceptions') ?>:
-
-
translate('File') ?>:
-
getFile() ?>:
getLine() ?>
-
translate('Message') ?>:
-
getMessage() ?>
-
translate('Stack trace') ?>:
-
getTraceAsString() ?>
getPrevious();
endwhile;
?>
translate('No Exception available') ?>
代码解释:
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
下面是代码内容:
translate('A 404 error occurred') ?>
message ?>
reason) && $this->reason): ?>
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;
}
?>
controller) && $this->controller): ?>
-
translate('Controller') ?>:
-
escapeHtml($this->controller) ?>
controller_class)
&& $this->controller_class
&& $this->controller_class != $this->controller
) {
echo '(' . sprintf($this->translate('resolves to %s'), $this->escapeHtml($this->controller_class)) . ')';
}
?>
display_exceptions) && $this->display_exceptions): ?>
exception) && $this->exception instanceof Exception): ?>
translate('Additional information') ?>:
exception); ?>
-
translate('File') ?>:
-
exception->getFile() ?>:
exception->getLine() ?>
-
translate('Message') ?>:
-
exception->getMessage() ?>
-
translate('Stack trace') ?>:
-
exception->getTraceAsString() ?>
exception->getPrevious();
if ($e) :
?>
translate('Previous exceptions') ?>:
-
-
translate('File') ?>:
-
getFile() ?>:
getLine() ?>
-
translate('Message') ?>:
-
getMessage() ?>
-
translate('Stack trace') ?>:
-
getTraceAsString() ?>
getPrevious();
endwhile;
?>
translate('No Exception available') ?>
代码解析:
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的相关提示信息找到出错的问题点。