添加文件:`/module/Album/src/Controller/AlbumController.php` 具體內容如下:
~~~
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\Album;
use Album\Form\AlbumForm;
class AlbumController extends AbstractActionController{
protected $albumTalbe;
public function indexAction(){
$paginator = $this->getAlbumTalbe()->fetchAll(true);
$paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page',1));
$paginator->setItemCountPerPage(5);
return new ViewModel(array('paginator'=>$paginator));
}
public function addAction(){
$form = new AlbumForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if($request->isPost()){
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
if($form->isValid()){
$album->exchangeArray($form->getData());
$this->getAlbumTalbe()->saveAlbum($album);
return $this->redirect()->toRoute('album');
}
}
return array('form'=>$form);
}
public function editAction(){
$id = (Int) $this->params()->fromRoute('id',0);
if(!$id){
return $this->redirect()->toRoute('album',array('action'=>'add'));
}
try{
$album = $this->getAlbumTalbe()->getAlbum($id);
}catch(\Exception $e){
return $this->redirect()->toRoute('album',array('action'=>'index'));
}
$form = new AlbumForm();
$form->bind($album);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if($request->isPost()){
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
if($form->isValid()){
$this->getAlbumTalbe()->saveAlbum($form->getData());
return $this->redirect()->toRoute('album');
}
}
return array('id'=>$id,'form'=>$form);
}
public function deleteAction(){
$id = (Int) $this->params()->fromRoute('id',0);
if(!$id){
return $this->redirect()->toRoute('album');
}
$request = $this->getRequest();
if($request->isPost()){
$del = $request->getPost('del','No');
if($del=='Yes'){
$id = (Int)$request->getPost('id');
$this->getAlbumTalbe()->deleteAlbum($id);
}
return $this->redirect()->toRoute('album');
}
return array('id'=>$id,'album'=>$this->getAlbumTalbe()->getAlbum($id));
}
public function getAlbumTalbe(){
if(!$this->albumTalbe){
$sm = $this->getServiceLocator();
$this->albumTalbe = $sm->get('Album\Model\AlbumTable');
}
return $this->albumTalbe;
}
}
~~~
代碼解釋:
public function indexAction(){} album默認訪問action,也是album列表action
public function addAction(){} 添加album 的 action
public function editAction(){} 編輯修改album的action
public function deleteAction(){} 刪除album 的action
lpublic function getAlbumTalbe(){} 設置數據庫網關
- 序言
- 第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章 結束語