模板是數據處理后的最終展現平臺,也是用戶操作與感知的入口。Album模塊使用的模板有4個:index.phtml , add.phtml , edit.phtml , delete.phtml , paginator.phtml 分別 列表、添加、修改、刪除、分頁導航
### 7.8.1 列表模板 index.phtml
添加文件:/module/Album/view/album/album/index.phtml,內容如下:
~~~
<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,內容如下:
~~~
<?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,內容如下:
~~~
<?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,內容如下:
~~~
<?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,內容如下:
~~~
<?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; ?>
~~~
經過以上的準備工作,接下可以通過 http://localhost/album 來打開album 列表的,打開的頁面結果如下所示:
~~~
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
~~~
點擊 列表上面的Add new album 可以進行 album 添加,頁面結果如下所示:
~~~
header
窗體頂端
Title窗體底端
Artist
footer
~~~
點擊列表右邊的 Edit 可以對album 的信息進行修改,頁面結果如下所示:
~~~
header
窗體頂端
Title窗體底端
Artist
footer
~~~
點擊列表右邊的 Delete 可以對album 記錄進行刪除,確認刪除頁面如下所示:
~~~
header
Delete album
Are you sure that you want to delete 'First album' by 'artist01'?
窗體頂端
窗體底端
footer
~~~
到此已經完成了對整個Album模塊的的構造及功能的實現,此實例雖然沒有實際的應用意義,但他已經完整的展示了在ZF2一個完整模塊的存在形式,以及與其他模塊同時并存且同時協同工作的具體應用,在進行具體項目開發的時候可以借鑒或參考此例以便開發出不同功能的模塊,使用項目模塊能夠共同協同工作。
- 序言
- 第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章 結束語