您的位置:首页 > 运维架构 > 网站架构

使用FleaPHP框架构建简单留言本应用

2007-08-22 18:21 871 查看
使用FleaPHP框架构建简单留言本应用

作者:heiyeluren
博客:http://blog.csdn.net/heiyeshuwu


【FleaPHP介绍】


FleaPHP是国产的一个MVC框架,目前主流的框架Zend FrameworkSymfonyCakePHP,国内还有FCSPlite等框架都是值得期待的。

我们看看官方的介绍:
FleaPHP 为开发者轻松、快捷的创建应用程序提供帮助。FleaPHP 框架简单、清晰,容易理解和学习,并且有完全中文化的文档和丰富的示例程序降低学习成本。使用 FleaPHP 框架开发的应用程序能够自动适应各种运行环境,并兼容 PHP4 和 PHP5。FleaPHP 的全名是 Fast-Lightweight-Extensible-Automatic PHP web application framework。

今天我简单的使用FleaPHP来构建一个简单的留言本程序来大概了解以下FleaPHP的运作机制。关于FleaPHP的更多信息访问官方网站:www.fleaphp.org(电信)www.fleaphp.net (网通)。
FleaPHP开发指南:http://www.fleaphp.net/index.php?q=guide



【构建留言本应用】

1. 数据表结构

留言本的要求比较简单,就是能够留言、显示留言,这么简单功能,看以下数据表结构:

--
-- 表的结构 `guestbook`
--
CREATE TABLE `guestbook` (
`id` int(10) NOT NULL auto_increment,
`nicker` varchar(50) NOT NULL default '',
`email` varchar(100) default NULL,
`url` varchar(100) default NULL,
`content` text NOT NULL,
`created` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) TYPE=MyISAM ;



2. 程序目录结构

整个留言本程序的结构是这样的:

/Fleaphp/ ----基本框架目录
/Guestbook ----留言本根目录
/Guestbook/Config ----配置文件目录
/Guestbook/Model ----模型层文件目录
/Guestbook/View ----显示层文件目录
/Guestbook/Controller ----控制层文件目录



3. 配置文件

我先构建配置文件,用来保存数据库的基本配置信息,配置文件路径是: /Guestbook/Config/DSN.config.php

<?php
/**
* DSN
* 数据源配置文件
*/

return array(
'dbDSN' => array(
'driver' => 'mysql',
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'test'
)
);

?>



4. 程序入口点(首页)

我们再来构建首页,就是我们所有应用的入口程序: /Guestbook/index.php

<?php
//======================================
// Name: Gueskbook
// Desc: first fleaphp application
//======================================

//包含文件
define("APP_DIR", dirname(__FILE__));
define("VIEW_DIR", APP_DIR ."/View/");
require_once("../FLEA/FLEA.php");

//载入DSN配置文件
$dsnConfigFile = './Config/DSN.config.php';
register_app_inf($dsnConfigFile);
import(dirname(__FILE__));

//执行
run();

?>

大致我们看就是配置APP_DIR常量,然后加载基本的FleaPHP框架文件和数据源配置信息,然后增加一个类搜索目录,最后执行run() 来运行整个程序。



5. 控制器(Controller)

现在来看看我们的主要东西,控制器(Controller): /Guestbook/Controller/Default.php

<?php
/**
* 缺省控制器
*/

class Controller_Default extends FLEA_Controller_Action
{
/**
* 留言本Model
*/
var $_modelGB;

/**
* 构造函数
*/
function Controller_Default(){
$this->_modelGB =& get_singleton("Model_GB");
}

/**
* 缺省action
*/
function actionIndex(){
$posts = $this->_modelGB->findAll(null, 'created DESC');
include("View/index.php");
}

/**
* 插入一条留言
*/
function actionCreate(){
$createArr = array(
'nicker' => htmlspecialchars($_POST[nicker]),
'email' => htmlspecialchars($_POST[email]),
'url' => htmlspecialchars($_POST
class Model_GB extends FLEA_Db_TableDataGateway
{
/**
* 数据表名称
*/
var $tableName = 'guestbook';

/**
* 数据表主键
*/
var $primaryKey = 'id';
}

?>

我们看,模型代码非常简单,就是一个继承了 FLEA_Db_TableDataGateway 类的 Model_GB 类,并且没有任何方法代码,只有两个属性,一个 $tableName 记录留言表的名称,$primaryKey 记录表里面的主键字段。



7. 显示层(View)

最后看看你我们用户能够查看到的显示层(View)的实现HTML: /Guestbook/View/index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<title>留言本</title>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="stylesheet" href="view/resource/css/gb2.css">
<style>
*{
margin:0;
padding:0;
}
body{
width:760px;
height:100%;
margin:10px auto 10px;
font-size:12px;
}
#main{
width:758px;
border:1px solid;
background-color:#eee;
}
#posts{
height:200px;
text-align:center;
font-size:12px;
}
#posts input, #posts textarea{
border:1px solid;
}

#posts h2{
padding:10px;
text-align:center;
font-size:14px;
}
#content-list{
text-align:center;
}
#content-list h2{
margin-left:70px;
padding:10px;
text-align:left;
font-size:14px;
}
.tbl-style{
display:block;
width:600px;
background-color:#ccc;
margin-bottom:10px;
padding:0 5px 0 5px;
border:1px solid green;
}
.td1-style{
width:38px;
text-align:left;
line-height:20px;

}
.td2-style{
width:100px;
text-align:left;
line-height:20px;
}
.td3-style{
width:450px;
text-align:left;
padding:10px;
}
</style>

<script language="javascript">
function checkForm(){
var o = document.getElementById("guestbook");
if (o.nicker.value == ''){
alert('一定要输入昵称哦。。。');
o.nicker.focus();
return false;
}
if (o.content.value == '' && o.content.value.length<10){
alert('恩,留言内容总要输吧,我觉得最少不能要少于10个字,不然咋叫留言捏。。。');
o.content.focus();
return false;
}
return true;
}
</script>
</head>

<body>
<div id="main">
<div id="posts">
<h2>留言本</h2>
<form action="<? echo $this->_url('create'); ?>" method="post" name="guestbook" id="guestbook" onsubmit="return checkForm()">
昵称:<input type="text" size="15" name="nicker" id="nicker" />    
邮箱:<input type="text" size="20" name="email" id="email" />    
网站:<input type="text" size="20" name="url" id="url" /><br /><br />
<textarea name="content" id="content" rows="10" cols="80"></textarea><br /><br />
<input type="submit" value="马上留言" />
</form>
</div>

<div id="content-list">
<h2>留言列表</h2>
<? foreach($posts as $gb){ ?>
<table class="tbl-style">
<tr>
<td class="td1-style">昵称:</td><td class="td2-style"><?=$gb[nicker]?></td>
<td class="td1-style">邮箱:</td><td class="td2-style"><?=$gb[email]?></td>
<td class="td1-style">网站:</td><td class="td2-style"><?=$gb[url]?></td>
</tr>
<tr>
<td class="td1-style">留言:</td><td class="td3-style" colspan="5"><?=$gb[content]?></td>
</tr>
</table>
<? } ?>

</div>
</div>
</body>
</html>



8. 实现效果图

总的结构就出来了,我们看以下运行结果的图片:

图片地址:[url=http://hiphotos.baidu.com/heiyeshuwu/pic/item/ea685bb5e82642cf37d3ca45.jpg]http://hiphotos.baidu.com/heiyeshuwu/pic/item/ea685bb5e82642cf37d3ca45.jpg" target=_blank>),
'content' => nl2br(htmlspecialchars($_POST[content])),
);

$this->_modelGB->create($createArr);
redirect($this->_url());
}
}

?>

我 们的控制器Controller_Default 是个缺省的控制器,它从 FLEA_Controller_Action 类继承了所有的属性和方法用于自己的控制。Controller_Default 类包含三个方法,构造函数是用来初始化一个Model类,actionIndex() 是缺省的动作方法,它从控制器 Model_GB 里把所有的留言提取出来,然后通过 View/index.php 文件来进行显示界面。actionCreate() 方法是创建一条留言的动作,就是构造好一个数据库,key是字段名,value是字段值的形式的数组,提交给 Model_GB 模型来进行处理,插入到数据库当中。



6. 模型层(Model)

我们再来看看模型层(Model)都实现一些什么代码:/Guestbook/Model/GB.php

<?php
//======================
// GuestBook Model
//======================

load_class("FLEA_Db_TableDataGateway");

class Model_GB extends FLEA_Db_TableDataGateway
{
/**
* 数据表名称
*/
var $tableName = 'guestbook';

/**
* 数据表主键
*/
var $primaryKey = 'id';
}

?>

我们看,模型代码非常简单,就是一个继承了 FLEA_Db_TableDataGateway 类的 Model_GB 类,并且没有任何方法代码,只有两个属性,一个 $tableName 记录留言表的名称,$primaryKey 记录表里面的主键字段。



7. 显示层(View)

最后看看你我们用户能够查看到的显示层(View)的实现HTML: /Guestbook/View/index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<title>留言本</title>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="stylesheet" href="view/resource/css/gb2.css">
<style>
*{
margin:0;
padding:0;
}
body{
width:760px;
height:100%;
margin:10px auto 10px;
font-size:12px;
}
#main{
width:758px;
border:1px solid;
background-color:#eee;
}
#posts{
height:200px;
text-align:center;
font-size:12px;
}
#posts input, #posts textarea{
border:1px solid;
}

#posts h2{
padding:10px;
text-align:center;
font-size:14px;
}
#content-list{
text-align:center;
}
#content-list h2{
margin-left:70px;
padding:10px;
text-align:left;
font-size:14px;
}
.tbl-style{
display:block;
width:600px;
background-color:#ccc;
margin-bottom:10px;
padding:0 5px 0 5px;
border:1px solid green;
}
.td1-style{
width:38px;
text-align:left;
line-height:20px;

}
.td2-style{
width:100px;
text-align:left;
line-height:20px;
}
.td3-style{
width:450px;
text-align:left;
padding:10px;
}
</style>

<script language="javascript">
function checkForm(){
var o = document.getElementById("guestbook");
if (o.nicker.value == ''){
alert('一定要输入昵称哦。。。');
o.nicker.focus();
return false;
}
if (o.content.value == '' && o.content.value.length<10){
alert('恩,留言内容总要输吧,我觉得最少不能要少于10个字,不然咋叫留言捏。。。');
o.content.focus();
return false;
}
return true;
}
</script>
</head>

<body>
<div id="main">
<div id="posts">
<h2>留言本</h2>
<form action="<? echo $this->_url('create'); ?>" method="post" name="guestbook" id="guestbook" onsubmit="return checkForm()">
昵称:<input type="text" size="15" name="nicker" id="nicker" />    
邮箱:<input type="text" size="20" name="email" id="email" />    
网站:<input type="text" size="20" name="url" id="url" /><br /><br />
<textarea name="content" id="content" rows="10" cols="80"></textarea><br /><br />
<input type="submit" value="马上留言" />
</form>
</div>

<div id="content-list">
<h2>留言列表</h2>
<? foreach($posts as $gb){ ?>
<table class="tbl-style">
<tr>
<td class="td1-style">昵称:</td><td class="td2-style"><?=$gb[nicker]?></td>
<td class="td1-style">邮箱:</td><td class="td2-style"><?=$gb[email]?></td>
<td class="td1-style">网站:</td><td class="td2-style"><?=$gb[url]?></td>
</tr>
<tr>
<td class="td1-style">留言:</td><td class="td3-style" colspan="5"><?=$gb[content]?></td>
</tr>
</table>
<? } ?>

</div>
</div>
</body>
</html>



8. 实现效果图

总的结构就出来了,我们看以下运行结果的图片:

图片地址:[url=http://hiphotos.baidu.com/heiyeshuwu/pic/item/ea685bb5e82642cf37d3ca45.jpg]http://hiphotos.baidu.com/heiyeshuwu/pic/item/ea685bb5e82642cf37d3ca45.jpg

(不知道为什么CSDN不能传图片了,所以放到我百度空间里)



三、结束

更多应用请参考FleaPHP的官方网站和下载源码中的示例程序,自己亲自尝试以下,也许,这个框架就是适合你的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: