實習小結十三:基於PHP ZF框架的文章管理模組
這是我最近兩三週做的一個Post模組,從資料庫到後臺,前臺,資料庫設計和總體設計都已經給我了,我也參考了不少已有模組的程式碼。剛開始7天,我用來實現post模組的增刪改查的基本功能,之後的時間,就是修改一些邏輯程式碼,以及增添新功能,就像小結12裡面寫的ajax功能之類的。
先給出這個module的整個程式碼框架吧:
列出來的程式碼檔案就是我所用到的程式碼,其中有些abstract為空,因為直接繼承了其他的類。
Controller裡面就是主要的action,在設定了路由檔案之後,在controller裡面寫出相應的action,在action中完成對前臺的傳參,設定相應的template等。
Form裡面就是在需要輸入資料,新增資料時,所用的表格,裡面有詳細的label引數,下圖就是基於PostAddForm的。
Model就是資料庫中對應的引數,在這邊定義清楚。
Table是對資料庫進行操作的各種方法,這邊參考了不少Zend Framework官網給出的Db方法,不是很難。在action裡面通過getTable呼叫這些方法。
接下來就是相應的頁面檔案了。post資料夾中是大體的頁面框架檔案,具體的小檔案部分在ctrl中,基本可以傳個partial到post大頁面中去。
這邊我把controller裡面,兩個稍微重要的方法貼出來。貼多了,以後自己看,估計也不知所云。
public function PostAction(){
if(!$this->userHasPermission('ADMIN', 'VIEW_PRODUCT'))
{
return $this->requirePermission('ADMIN', 'VIEW_PRODUCT');
}
$page = $this->getRequest()->getQuery('page', 1);
$condition = array();
$type_name = 'post';
if(isset($_GET['post_status']) && $_GET['post_status'] != 'null') {
$status = $_GET['post_status'];
$condition['post_status'] = (int)$_GET['post_status'];
}else{
$condition['post_status'] = null;
}
if(isset($_GET['query'])) {
$condition['title'] = $_GET['query'];
}
else{
$condition['title'] = null;
}
if(isset($_GET['category'])) {
$condition['category'] = $_GET['category'];
}
else{
$condition['category'] = null;
}
if(isset($_GET['author'])) {
$condition['author'] = $_GET['author'];
}
else{
$condition['author'] = null;
}
$posts = $this->getPostTable()->getPaginator($condition);
$posts->setItemCountPerPage(10);
$posts->setCurrentPageNumber($page);
$vm = new ViewModel(array(
'post_paginator' => $posts,
));
$vm->setTemplate('post/post-index');
$this->layout()->selectedTab = 'post-list';
return $vm;
}
public function addPostAction(){
if(!$this->userHasPermission('ADMIN', 'VIEW_PRODUCT'))
{
return $this->requirePermission('ADMIN', 'VIEW_PRODUCT');
}
$user_service = $this->getServiceLocator()->get('UserService');
$table = $this->getPostTable();
$config = $this->getServiceLocator()->get('config');
$sm = $this->getServiceLocator();
$request = $this->getRequest();
$form = new \Post\Form\PostAddForm(array(
'adapter' => $sm->get('Zend\Db\Adapter\Adapter'),
'user_service' => $user_service,
));
$author_valid = true;
if($request->isPost())
{
$form->setData($request->getPost());
if ($form->isValid()) {
$post_array = array();
$post = new Post($this->getRequest()->getPost()->toArray());
$user = $this->getServiceLocator()->get('UserService')->getUserById($post->author_id);
if ($user) {
$author = $user->username;
}
else
$author = null;
foreach ($post as $key => $value) {
if ($key == 'post_featured_img') {
$json_arr = json_decode($value,true);
$value = '/img'. '/' . substr($json_arr['name'],0,6) . '/' . $json_arr['name'] . '.' . $json_arr['type'];
}
$post_array[$key] = $value;
}
$post_array['author'] = $author;
if (is_null($post_array['author'])) {
$author_valid = false;
}else{
$postId = $table->addPost($post_array);
$this->redirect()->toUrl("/post");
}
}
}
在程式碼中可以看出,關於圖片是使用Json陣列返回的,這是程式碼庫裡原來就封裝好的Image Element的返回值,感覺很方便。
估計接下來,還得繼續改改這個模組,剛開始做的時候,財哥跟我講這個,我覺得很抽象,根本想象不出來這個怎麼做,現在做出來,功能也都實現了,感覺蠻有成就感的,在部落格裡面算是紀念一下,這兩週來,這個module給我帶來的好壞心情吧。