下面以一个简单的留言簿为例,介绍一下ZF中MVC模式的整体开发流程。
4.1 系统入口index.php
如果不对项目的路径、异常、及Router、Dispatcher等做特殊的设置,index.php这个入口文件就可以非常简单地只写两句话:
<?php
require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('./app/controllers');
?>
4.2 控制器MsgController.php
简单的留言簿只有留言和查看的功能,也就是新增和显示。其代码如下:
<?php
require_once('Zend/Controller/Action.php');
require_once('app/models/MsgDb.php');
require_once 'Zend/Filter/HtmlEntities.php'; //网页标签过滤器
class MsgController extends Zend_Controller_Action {
private $db; //数据库操作类
function init(){
$this->db = new MsgDb(); //在初始化的时候实例化一个}
//显示所有留言
public function showAction(){
//分页}
$rows = $this->db->getCount(); //总记录数
$rowsPerPage = 5; //每页记录数
$pages = ceil($rows / $rowsPerPage); //总页数
$curPage = 1; //当前页
if($this->_request->getParam('curPage'))
$curPage = $this->_request->getParam('curPage');
//查询数据、显示
$this->view->msgs = $this->db->getMsgs($curPage, $rowsPerPage); //调用数据库操作类的方法直接得到所有数据,为Rowset格式
$this->view->title = '留言簿';
$this->view->pages = $pages;
$this->view->curPage = $curPage;
//新增留言
public function insertAction(){
$html = new Zend_Filter_HtmlEntities(null,'GB2312'); //建立标签过滤器}
$content = $html->filter($this->_request->getParam('content')); //对标签进行过滤
//将所有数据放入数组
$row = array('useremail'=>$this->_request->getParam('useremail'),
'title'=>$this->_request->getParam('title'),
'userip'=>$_SERVER['REMOTE_ADDR'],
'content'=>$content,
'mtime'=>date('Y-m-d H:i:s'));
if($id = $this->db->insertMsg($row)){ //调用数据库类的插入方法
$this->view->insertMsg = "留言 $id 成功";}
$this->_forward('show'); //再转到显示Action显示页面
}
?>
在控制器文件中,不进行任何的数据库操作,数据库业务逻辑都交给Model去处理,在Controller中只调用就可以。在上述代码中,一共定义有3个方法,第一个init方法在实例化控制器时会被默认调用,所以在该方法中实例化一个数据库的操作类对象$db。
在showAction方法中,实现了对数据库内容的分页显示;insertAction方法实现内容过滤和入库操作。
4.3 模型类MsgDb.php
在进行数据库操作之前,可以首先将数据库的一些配置信息存入配置文件config.ini中,内容如下:
[database]
db.adapter = PDO_MYSQL
db.config.host = localhost
db.config.port = 3306
db.config.username = root
db.config.password = root
db.config.dbname = test
然后将这个配置信息读成一个config对象:
$config = new Zend_Config_Ini('config.ini', 'database');
这样就可以生成数据库对象了:
$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
$db->query("set names gbk"); //处理汉字问题
Zend_Db_Table::setDefaultAdapter($db); //设置表的默认适配器
有了上面最后一句,就可以考虑使用Zend Framework中的Table类库,可以将数据库中的messages表映射为Messages类,由于表名、字段名没有什么变化,该类简单到只有类声明:
<?php
require_once('Zend/Db/Table.php');
class Messages extends Zend_Db_Table {}
?>
有了上面的这些准备工作,其余对数据库的访问将变的很简单:
//返回留言表中的所有记录
public function getAllMsgs(){
$tMsgs = new Messages();}
//第一个字段为where,没有条件时用null。第二个表示排序
$msgs = $tMsgs->fetchAll(null,'id desc');
return $msgs;
//返回留言表中的指定的记录行
public function getMsgs($curPage, $rowsPerPage){
$tMsgs = new Messages();}
$msgs = $tMsgs->fetchAll(null,'id desc', $rowsPerPage,
($curPage-1) * $rowsPerPage ) ;
return $msgs;
//新增一条记录
public function insertMsg($row){
$msg = new Messages();}
//直接插入数据表中。返回值为新纪录的id
return $msg->insert($row);
//得到记录总数
public function getCount(){
$tMsgs = new Messages();}
$db = $tMsgs->getAdapter();
return $db->fetchOne("select count(*) from messages");
4.4 视图对象show.phtml
视图页面比较简单,主要分为循环显示留言、留言分页、发表留言表单三部分:
<html>
<head>
<meta name="GENERATOR" content="Zend Studio" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title><?php echo $this->title;?></title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
<h2 align="center"><?php echo $this->title;?></h2>
<h4 align="center"><?php echo $this->insertMsg;?></h4>
<hr width="90%">
<?php
//输出留言
foreach ($this->msgs as $msg){
echo "
<table align='center' width='90%' border='1'>
<tr>
<td width='30%'>{$msg->id}</td>
<td>{$msg->title}</td>
</tr>
<tr>
<td>{$msg->useremail}<br>{$msg->userip}<br>{$msg->mtime}</td>
<td>{$msg->content}</td>
</tr>
</table>
<br>
";
}
//输出分页
echo "<div align='center'>";
for($i = 1; $i <= $this->pages; $i ++){
if($i == $this->curPage)
echo "$i ";
else
echo "<a href='?curPage=$i'>$i</a> ";
}
echo "</div>";
?>
<hr width="90%">
<form action="insert" method="POST">
<table align="center" width="90%">
<tr>
<td width='20%' align="right">邮箱:</td>
<td><input type="text" name="useremail" value=""/> </td>
</tr>
<tr>
<td align="right">标题:</td>
<td><input type="text" name="title" value="" size="80"/> </td>
</tr>
<tr>
<td align="right">内容:</td>
<td><textarea name="content" cols="80" rows="5"></textarea> </td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="发表留言"/> </td>
</tr>
</table>
</form>
</body>
</html>
4.4 测试
代码开发出来后,就可以通过http://localhost/zfMsg/msg/show来访问了。效果如下:




