模板是數(shù)據(jù)處理后的最終展現(xiàn)平臺(tái),也是用戶(hù)操作與感知的入口。Album模塊使用的模板有4個(gè):index.phtml , add.phtml , edit.phtml , delete.phtml , paginator.phtml 分別 列表、添加、修改、刪除、分頁(yè)導(dǎo)航
### 7.8.1 列表模板 index.phtml
添加文件:/module/Album/view/album/album/index.phtml,內(nèi)容如下:
~~~
<table>
<tr>
<th><?php echo $this->translate("Title") ?></th>
<th><?php echo $this->translate("Artist") ?></th>
<th><a href="<?php echo $this->url('album', array('action' => 'add')); ?>"><?php echo $this->translate("Add new album") ?></a></th>
</tr>
<?php foreach ($paginator as $album) : ?>
<tr>
<td><?php echo $this->escapeHtml($album->title); ?></td>
<td><?php echo $this->escapeHtml($album->artist); ?></td>
<td>
<a href="<?php echo $this->url('album', array('action' => 'edit', 'id' => $album->id));
?>"><?php echo $this->translate("Edit") ?></a>
<a href="<?php echo $this->url('album', array('action' => 'delete', 'id' => $album->id));
?>"><?php echo $this->translate("Delete") ?></a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php
echo $this->paginationControl($this->paginator,'sliding',array('partial/paginator.phtml','Album'),array('route'=>'album'));
?>
~~~
### 7.8.2 列表模板 add.phtml
添加文件:/module/Album/view/album/album/add.phtml,內(nèi)容如下:
~~~
<?php
$form = $this->form;
$form->setAttribute('action',$this->url('album',array('action'=>'add')));
echo $this->form()->openTag($form);
echo $this->formCollection($this->form);
echo $this->form()->closeTag();
?>
~~~
### 7.8.3 列表模板 edit.phtml
添加文件:/module/Album/view/album/album/edit.phtml,內(nèi)容如下:
~~~
<?php
$form = $this->form;
$form->setAttribute('action',$this->url('album',array('action'=>'edit','id'=>$this->id)));
echo $this->form()->openTag($form);
echo $this->formCollection($this->form);
echo $this->form()->closeTag();
?>
~~~
### 7.8.4 列表模板 delete.phtml
添加文件:/module/Album/view/album/album/delete.phtml,內(nèi)容如下:
~~~
<?php
$title = 'Delete album';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>Are you sure that you want to delete
'<?php echo $this->escapeHtml($album->title); ?>' by
'<?php echo $this->escapeHtml($album->artist); ?>'?
</p>
<?php
$url = $this->url('album', array(
'action' => 'delete',
'id' => $this->id,
));
?>
<form action="<?php echo $url; ?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo (int) $album->id; ?>" />
<input type="submit" name="del" value="Yes" />
<input type="submit" name="del" value="No" />
</div>
</form>
~~~
### 7.8.5 列表模板 paginator.phtml
添加文件:/module/Album/view/partial/paginator.phtml,內(nèi)容如下:
~~~
<?php if ($this->pageCount): ?>
<div class="pagination pagination-centered">
<ul>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page=<?php echo $this->previous;?>"><<</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
<<
</a>
</li>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page=<?php echo $page; ?>">
<?php echo $page; ?>
</a>
</li>
<?php else: ?>
<li class="active">
<a href="#"><?php echo $page; ?></a>
</li>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<li>
<a href="<?php echo $this->url($this->route); ?>?page=<?php echo $this->next; ?>">
>>
</a>
</li>
<?php else: ?>
<li class="disabled">
<a href="#">
>>
</a>
</li>
<?php endif; ?>
</ul>
</div>
<?php endif; ?>
~~~
經(jīng)過(guò)以上的準(zhǔn)備工作,接下可以通過(guò) http://localhost/album 來(lái)打開(kāi)album 列表的,打開(kāi)的頁(yè)面結(jié)果如下所示:
~~~
header
Title
Artist
Add new album
First album
artist01
EditDelete
Second album
artist02
EditDelete
Third album
artist03
EditDelete
fourth album
artist04
EditDelete
Fifth album
artist05
EditDelete
<< 1 2 >>
footer
~~~
點(diǎn)擊 列表上面的Add new album 可以進(jìn)行 album 添加,頁(yè)面結(jié)果如下所示:
~~~
header
窗體頂端
Title窗體底端
Artist
footer
~~~
點(diǎn)擊列表右邊的 Edit 可以對(duì)album 的信息進(jìn)行修改,頁(yè)面結(jié)果如下所示:
~~~
header
窗體頂端
Title窗體底端
Artist
footer
~~~
點(diǎn)擊列表右邊的 Delete 可以對(duì)album 記錄進(jìn)行刪除,確認(rèn)刪除頁(yè)面如下所示:
~~~
header
Delete album
Are you sure that you want to delete 'First album' by 'artist01'?
窗體頂端
窗體底端
footer
~~~
到此已經(jīng)完成了對(duì)整個(gè)Album模塊的的構(gòu)造及功能的實(shí)現(xiàn),此實(shí)例雖然沒(méi)有實(shí)際的應(yīng)用意義,但他已經(jīng)完整的展示了在ZF2一個(gè)完整模塊的存在形式,以及與其他模塊同時(shí)并存且同時(shí)協(xié)同工作的具體應(yīng)用,在進(jìn)行具體項(xiàng)目開(kāi)發(fā)的時(shí)候可以借鑒或參考此例以便開(kāi)發(fā)出不同功能的模塊,使用項(xiàng)目模塊能夠共同協(xié)同工作。
- 序言
- 第1章 Zend Framework2 簡(jiǎn)介
- 1.1 Zend Framework2 簡(jiǎn)介
- 1.2 下載安裝
- 1.3 搭建開(kāi)發(fā)環(huán)境
- 第2章 創(chuàng)建ZF2項(xiàng)目
- 2.1 新建一個(gè)項(xiàng)目
- 2.2 配置網(wǎng)站
- 2.3 偽靜態(tài) .htaccess文件
- 2.4 添加啟動(dòng)/入口文件
- 2.5 添加全局配置文件
- 2.6 添加自動(dòng)加載文件 init_autoloader.php
- 2.7 IndexController 控制器
- 第3章 創(chuàng)建模塊文件
- 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 服務(wù)管理器
- 3.2.5 translator 翻譯器
- 3.2.6 navigation 導(dǎo)航條
- 第4章 創(chuàng)建控制器
- 4.1 控制器簡(jiǎn)介
- 4.2 新建控制器
- 4.3 添加控制器的Action
- 第5章 創(chuàng)建視圖模板
- 5.1 創(chuàng)建模板
- 5.2 模板配置
- 5.3 編寫(xiě)布局和錯(cuò)誤異常模板
- 5.4 編寫(xiě)Action 對(duì)應(yīng)的模板文件
- 5.5 訪(fǎng)問(wèn) IndexAction
- 第6章 創(chuàng)建模型
- 6.1 ORM 對(duì)象映射法
- 6.2 使用分頁(yè)導(dǎo)航
- 6.3 自定模型
- 6.4 章節(jié)總結(jié)
- 第7章 實(shí)例應(yīng)用
- 7.1 建立Album 模塊
- 7.2 添加模塊文件
- 7.3 添加模塊配置文件
- 7.4 創(chuàng)建數(shù)據(jù)表 album
- 7.5 添加模型文件
- 7.6 添加表單 AlbumForm
- 7.7 添加控制器 AlbumController
- 7.8 添加模板文件
- 第8章 用戶(hù)認(rèn)證
- 8.1 建立數(shù)據(jù)表
- 8.2 新建認(rèn)證類(lèi)
- 8.3 引用認(rèn)證類(lèi)
- 第9章 結(jié)束語(yǔ)
