走过平凡人生、留下平凡足迹 注册 | 登陆

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

【构建留言本应用】

1. 数据表结构

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

php代码
  1. --   
  2. -- 表的结构 `guestbook`  
  3. --   
  4. CREATE TABLE `guestbook` (  
  5.   `id` int(10) NOT NULL auto_increment,  
  6.   `nicker` varchar(50) NOT NULL default '',  
  7.   `email` varchar(100) default NULL,  
  8.   `url` varchar(100) default NULL,  
  9.   `content` text NOT NULL,  
  10.   `created` datetime NOT NULL default '0000-00-00 00:00:00',  
  11.   PRIMARY KEY  (`id`)  
  12. ) TYPE=MyISAM ;  

2. 程序目录结构


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

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


3. 配置文件

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

PHP代码
  1. <?php  
  2. /** 
  3. * DSN 
  4. * 数据源配置文件 
  5. */  
  6.   
  7. return array(  
  8.     'dbDSN' => array(  
  9.         'driver'    => 'mysql',  
  10.         'host'      => 'localhost',  
  11.         'login'     => 'root',  
  12.         'password'  => '',  
  13.         'database'   => 'test'  
  14.     )  
  15. );  
  16.   
  17. ?>  

4. 程序入口点(首页)

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

PHP代码
  1. <?php  
  2. //======================================  
  3. // Name:     Gueskbook  
  4. // Desc:     first fleaphp application  
  5. //======================================  
  6.   
  7. //包含文件  
  8. define("APP_DIR", dirname(__FILE__));  
  9. define("VIEW_DIR", APP_DIR ."/View/");  
  10. require_once("../FLEA/FLEA.php");  
  11.   
  12. //载入DSN配置文件  
  13. $dsnConfigFile = './Config/DSN.config.php';  
  14. register_app_inf($dsnConfigFile);  
  15. import(dirname(__FILE__));  
  16.   
  17. //执行  
  18. run();  
  19.   
  20. ?>  

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



5. 控制器(Controller)

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

PHP代码
  1. <?php  
  2. /** 
  3. * 缺省控制器 
  4. */  
  5.   
  6. class Controller_Default extends FLEA_Controller_Action  
  7. {  
  8.     /** 
  9.      * 留言本Model 
  10.      */  
  11.     var $_modelGB;  
  12.      
  13.     /** 
  14.      * 构造函数 
  15.      */  
  16.     function Controller_Default(){  
  17.         $this->_modelGB =& get_singleton("Model_GB");  
  18.     }  
  19.      
  20.     /** 
  21.      * 缺省action 
  22.      */  
  23.     function actionIndex(){  
  24.         $posts = $this->_modelGB->findAll(null, 'created DESC');  
  25.         include("View/index.php");  
  26.     }  
  27.      
  28.     /** 
  29.      * 插入一条留言 
  30.      */  
  31.     function actionCreate(){  
  32.         $createArr = array(  
  33.             'nicker'    => htmlspecialchars($_POST[nicker]),  
  34.             'email'     => htmlspecialchars($_POST[email]),  
  35.             'url'        => htmlspecialchars($_POST[url]),  
  36.             'content'    => nl2br(htmlspecialchars($_POST[content])),  
  37.         );     
  38.           
  39.         $this->_modelGB->create($createArr);  
  40.         redirect($this->_url());  
  41.     }  
  42. }  
  43.   
  44. ?>  


我们的控制器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代码
  1. <?php  
  2. //======================  
  3. //  GuestBook Model  
  4. //======================  
  5.   
  6. load_class("FLEA_Db_TableDataGateway");  
  7.   
  8. class Model_GB extends FLEA_Db_TableDataGateway  
  9. {  
  10.     /** 
  11.      * 数据表名称 
  12.      */  
  13.     var $tableName = 'guestbook';  
  14.      
  15.     /** 
  16.      * 数据表主键 
  17.      */  
  18.     var $primaryKey = 'id';     
  19. }  
  20.   
  21. ?>  

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



7. 显示层(View)

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

PHP代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD xhtml 1.0 Transitional//EN">  
  2. <html>  
  3. <head>  
  4. <title>留言本</title>  
  5. <meta name="keywords" content="">  
  6. <meta name="description" content="">  
  7. <link rel="stylesheet" href="view/resource/css/gb2.css">  
  8. <style>  
  9. *{  
  10.     margin:0;  
  11.     padding:0;  
  12. }  
  13. body{  
  14.     width:760px;  
  15.     height:100%;  
  16.     margin:10px auto 10px;  
  17.     font-size:12px;  
  18. }  
  19. #main{  
  20.     width:758px;     
  21.     border:1px solid;  
  22.     background-color:#eee;    
  23. }  
  24. #posts{  
  25.     height:200px;  
  26.     text-align:center;  
  27.     font-size:12px;  
  28. }  
  29. #posts input, #posts textarea{  
  30.     border:1px solid;  
  31. }  
  32.   
  33. #posts h2{  
  34.     padding:10px;  
  35.     text-align:center;  
  36.     font-size:14px;  
  37. }  
  38. #content-list{  
  39.     text-align:center;  
  40. }  
  41. #content-list h2{  
  42.     margin-left:70px;  
  43.     padding:10px;  
  44.     text-align:left;  
  45.     font-size:14px;  
  46. }  
  47. .tbl-style{  
  48.     display:block;  
  49.     width:600px;  
  50.     background-color:#ccc;  
  51.     margin-bottom:10px;  
  52.     padding:0 5px 0 5px;  
  53.     border:1px solid green;  
  54. }  
  55. .td1-style{  
  56.     width:38px;  
  57.     text-align:left;  
  58.     line-height:20px;  
  59.   
  60. }  
  61. .td2-style{  
  62.     width:100px;  
  63.     text-align:left;  
  64.     line-height:20px;  
  65. }  
  66. .td3-style{  
  67.     width:450px;  
  68.     text-align:left;  
  69.     padding:10px;  
  70. }  
  71. </style>  
  72.   
  73. <script language="javascript">  
  74. function checkForm(){  
  75.     var o = document.getElementById("guestbook");  
  76.     if (o.nicker.value == ''){  
  77.         alert('一定要输入昵称哦。。。');  
  78.         o.nicker.focus();  
  79.         return false;  
  80.     }  
  81.     if (o.content.value == '' && o.content.value.length<10){  
  82.         alert('恩,留言内容总要输吧,我觉得最少不能要少于10个字,不然咋叫留言捏。。。');  
  83.         o.content.focus();  
  84.         return false;  
  85.     }  
  86.     return true;  
  87. }  
  88. </script>  
  89. </head>  
  90.   
  91. <body>  
  92. <div id="main">  
  93.     <div id="posts">  
  94.         <h2>留言本</h2>  
  95.         <form action="<? echo $this->_url('create'); ?>" method="post" name="guestbook" id="guestbook" onsubmit="return checkForm()">  
  96.             昵称:<input type="text" size="15" name="nicker" id="nicker" />      
  97.             邮箱:<input type="text" size="20" name="email" id="email" />      
  98.             网站:<input type="text" size="20" name="url" id="url" /><br /><br />  
  99.             <textarea name="content" id="content" rows="10" cols="80"></textarea><br /><br />  
  100.             <input type="submit" value="马上留言" />  
  101.         </form>  
  102.     </div>  
  103.   
  104.     <div id="content-list">  
  105.         <h2>留言列表</h2>     
  106.         <? foreach($posts as $gb){ ?>  
  107.         <table class="tbl-style">  
  108.             <tr>  
  109.                 <td class="td1-style">昵称:</td><td class="td2-style"><?=$gb[nicker]?></td>  
  110.                 <td class="td1-style">邮箱:</td><td class="td2-style"><?=$gb[email]?></td>  
  111.                 <td class="td1-style">网站:</td><td class="td2-style"><?=$gb[url]?></td>  
  112.             </tr>  
  113.             <tr>  
  114.                 <td class="td1-style">留言:</td><td class="td3-style" colspan="5"><?=$gb[content]?></td>  
  115.             </tr>  
  116.         </table>  
  117.         <? } ?>  
  118.           
  119.     </div>  
  120. </div>  
  121. </body>  
  122. </html>  

8. 实现效果图

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

[attach=1]


三、结束

更多应用请参考FleaPHP的官方网站和下载源码中的示例程序,自己亲自尝试以下,也许,这个框架就是适合你的。

 

Tags: fleaphp, php, mysql, 框架, ajax

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5

发表评论

评论内容 (必填):