本文章參考《php頂級架構zendframe開發實戰》第四章內容,並完整實現...
首先將用到的css檔案下載下來:http://download.csdn.net/download/unityoxb/4058802
解壓後把default和common兩個檔案複製到public/skins目錄下;
1、用到的資料庫檔案mysql.sql
create table if not exists `core_pages`( `id` int(10) unsigned not null auto_increment comment '頁面唯一ID', `cid` int(10) unsigned not null default '0' comment '分類ID', `uid` int(10) unsigned not null default '0' comment '使用者ID', `title` varchar(255) not null comment '頁面標題', `body` text not null comment '內容', `status` tinyint(4) not null default '1' comment '是否發布', `createtime` int(11) not null default '0' comment '建立頁面時間', `updatetime` int(11) not null default '0' comment '修改頁面時間', `comment` tinyint(4) not null default '0' comment '頁面是否評論功能', `start` tinyint(4) not null default '0' comment '頁面層級', `top` tinyint(4) not null default '0' comment '置頂', primary key (`id`))ENGINE=InnoDB default charset=utf8;
開啟mysql,使用source mysql.sql即可建立表結構
2、配置application.ini檔案(hahacom/applicaton/configs)
主要配置zend Framework串連mysql
[development : production]phpSettings.display_startup_errors = 1phpSettings.display_errors = 1resources.frontController.params.displayExceptions = 1resources.db.adapter = "PDO_MYSQL"resources.db.params.host = "localhost"resources.db.params.username = "root"resources.db.params.password = "root"resources.db.params.dbname = "test" --這是資料名稱resources.db.isDefaultTableAdapter = "TRUE"resources.db.params.driver_options.1002 = "SET NAMES UTF8;"
3、public/index.php
// Define application environmentdefined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development')); //修改成測試環境
4、建立文章展示model(model主要儲存資料模型類似javabean,或者從資料庫擷取資料並儲存在記憶體裡)
執行命令:zf create model page
會自動產生一個models/Page.php檔案
getAdapter(); $select = $db->select(); /*if($where != null) { //$select->where(' star = ? ', $where); //$sql = $db->quoteInto("select * from `core_pages` where `star`= ?", $where); //$result = $db->query($sql); $select->from('core_pages','*')->where('star = ?', $where)->limit(1); }*/ $select->from('core_pages','*'); if(count($where)>0) { foreach($where as $key=>$value) $select->where($key.' = ?',$value); } //$row = $result->fetch(); $row = $db->fetchAll($select); if($row) { return $row; } else { echo "================="; return null; } } public function getPages($where = null) { $db = Zend_Db_Table::getDefaultAdapter(); if(is_numeric($where)) { //$row = $db->find($where)->current(); $select = $db->select(); $select->from('core_pages','*'); $select->where('id = ?', $where); $row = $db->fetchRow($select); } if(is_array($where) && count($where)>0) { $select = $db->select(); $select->from('core_pages','*'); foreach($where as $key=>$value){ $select->where($key.'=?', $value); } $row = $db->fetchAll($select); } if($row) { return $row; } else { echo "================="; return null; } }}?>
5、建立controller
執行命令: zf create controller news 會自動產生controllers/NewsController.php
1, 'comment'=>1); $newsStar = $modelPage->getPage($where); //print_r($newsStar); $this->view->News = $newsStar; //$this->view->name = "hahaha"; }}
執行命令: zf create controller page 和 zf create action detail page
會自動產生 controllers/PageController.php
_request->getParam('id'); $modelPage = new Application_Model_Page($id);//if($modelPage == null) //print_r('==============================');//print_r($id);//print_r($modelPage); $page = $modelPage->getPages($id); $this->view->page = $page; }}
5、接下來建立視圖檔案
/views/scripts/news/index.phtml
".$this->News[0]['title']."";echo $this->News[0]['body'];//echo $this->name;if($this->News){ /*echo "
"; // print_r($this->News); foreach($this->News as $val) { echo "
- "."".$val['title'].""."
"; } echo "
"; */ echo "
"; echo $this->partialLoop('row_pages.phtml', $this->News); echo "
";}?>
/views/scripts/row_pages.phtml
id; ?>">title; ?> 發表時間: createtime); ?>
/views/scripts/page/detail.phtml
".$this->page['title'].""; echo "發表:".date('Y-m-d', $this->page['createtime']).""; echo ""; echo $this->page['body'];?>
運行:
點選連結:
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
以上就介紹了php利用zendframework編程執行個體,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。