為了使所有的模板文件能夠看起來足夠簡潔和便于理解,在模板文件的編寫上將盡量的用少的代碼及多的常用代碼來進行講解,而且在模板文件中較多的代碼仍為普通的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的相關提示信息找到出錯的問題點。
- 序言
- 第1章 Zend Framework2 簡介
- 1.1 Zend Framework2 簡介
- 1.2 下載安裝
- 1.3 搭建開發環境
- 第2章 創建ZF2項目
- 2.1 新建一個項目
- 2.2 配置網站
- 2.3 偽靜態 .htaccess文件
- 2.4 添加啟動/入口文件
- 2.5 添加全局配置文件
- 2.6 添加自動加載文件 init_autoloader.php
- 2.7 IndexController 控制器
- 第3章 創建模塊文件
- 3.1 Module 文件
- 3.2 module.config 文件
- 3.2.1 router 路由配置
- 3.2.2 controllers控制器配置
- 3.2.3 view_manager 視圖管理器
- 3.2.4 service_manager 服務管理器
- 3.2.5 translator 翻譯器
- 3.2.6 navigation 導航條
- 第4章 創建控制器
- 4.1 控制器簡介
- 4.2 新建控制器
- 4.3 添加控制器的Action
- 第5章 創建視圖模板
- 5.1 創建模板
- 5.2 模板配置
- 5.3 編寫布局和錯誤異常模板
- 5.4 編寫Action 對應的模板文件
- 5.5 訪問 IndexAction
- 第6章 創建模型
- 6.1 ORM 對象映射法
- 6.2 使用分頁導航
- 6.3 自定模型
- 6.4 章節總結
- 第7章 實例應用
- 7.1 建立Album 模塊
- 7.2 添加模塊文件
- 7.3 添加模塊配置文件
- 7.4 創建數據表 album
- 7.5 添加模型文件
- 7.6 添加表單 AlbumForm
- 7.7 添加控制器 AlbumController
- 7.8 添加模板文件
- 第8章 用戶認證
- 8.1 建立數據表
- 8.2 新建認證類
- 8.3 引用認證類
- 第9章 結束語