[lightphp入门教程]使用lightphp框架开发小型blog系统

October 7, 2009 – 6:33 pm

lightphp适合熟练的phper进行企业级应用开发,如果单纯论及代码自动生成,ORM模型和yml载入之类功能,lightphp无法跟一些成熟的框架(例如symfony)相比。我们的目的是,按需定制,性能优先。
如果做访问量不高且功能简单的应用,lightphp就不是明智的选择,建议使用成熟的框架和数据操作方案,例如:symfony + Doctrine。但是一个入门教程又不可能写得太复杂,所以还是用构建小型blog应用做例子,麻雀虽小,但五脏俱全,基本涵盖了lightphp的开发流程,希望起到抛砖引玉的作用。
(本教程基于linux + apache + mysql + php平台)

1.下载安装lightphp

zip

tar.gz
解压缩lightphp1.0.zip到你的web目录,配置虚拟主机为:

  1. <VirtualHost *:80>
  2.   DocumentRoot "/home/htdocs/framework/lightphp/blog/0.2/control"
  3.   ServerName blog.lightphp.com
  4.   DirectoryIndex index.php
  5.   <Directory "/home/htdocs/framework/lightphp/blog/0.2/control">
  6.     Options Indexes FollowSymLinks
  7.     AllowOverride All
  8.     Order deny,allow
  9.     Allow from all
  10.   </Directory>
  11. </VirtualHost>

修改/etc/hosts,加入:

  1. 127.0.0.1 blog.lightphp.com

重新启动网络和apache服务器,在浏览器里输入http://blog.lightphp.com既可以访问到lightphp的欢迎界面。

2.blog数据库设计

数据库名就叫:lightphp_blog,由于功能实现很简单,所以我们只需要两个表即可实现。

文章表

  1. lightphp_article{
  2.   recordID:(int(8))
  3.   title:(varchar(100))
  4.   body:(text)
  5.   createDate:(int(10))
  6.   updateDate(int(10))
  7.   categoryID:(int(10))
  8. }

标签表

  1. lightphp_category{
  2.   recordID:(int(8))
  3.   categoryName:(varchar(100)) 
  4. }

3.开始写代码

I.明确功能需求,划分模块

由于这是一个小型blog系统,所以功能会变得很简单。
*选择分类发表文章;
*显示所有文章
*编辑文章
*删除文章

II.定义url重写规则(如果你不在乎index.php/module=xxx&action=xxx之类不友好的url,此部分可以略过)

打开control/.htaccess文件添加url重写规则如下:

  1. #添加文章
  2. RewriteRule ^add index.php?module=article&action=add
  3. #编辑文章
  4. RewriteRule ^edit/(\d+) index.php?module=article&action=edit&articleID=$1
  5. #删除文章
  6. RewriteRule ^del/(\d+) index.php?module=article&action=del&articleID=$1
  7. #显示所有文章
  8. RewriteRule ^listall/(\d+) index.php?module=article&action=listall&page=$1
  9. RewriteRule ^listall index.php?module=article&action=listall
  10. #查看单个文章
  11. RewriteRule ^show/(\d+) index.php?module=article&action=show&articleID=$1

III.规划程序结构

由于功能集中在对文章的操作上,所以我们创建一个article模块,即在目录module目录下创建article文件夹,然后在article目录里创建listall.php。接着在目录view下创建article文件夹,继续在/view/article/里创建listall.php模板文件,用于存放动作程序对应的模板文件。以此类推,我们按照这个规则创建了add、edit、del和show对应的动作和模板文件,最后还需要在config/custom.config.php中加入以下配置:

  1. $lightphp['modules']['article'] = array('listall',
  2.                                         'add',
  3.                                         'edit',
  4.                                         'del',
  5.                                         'show');

项目使用mysql数据库,为了后面的扩展性,我们封装一个mysql数据操作类。
在config/custom.config.php中继续加入数据库连接代码:

  1. load_lib('mysql.db.class');
  2. $dbconfig = array('host' => 'localhost',
  3.                   'port' => '',
  4.                   'usr' => 'root',
  5.                   'pwd' => '',
  6.                   'dbname' => 'lightphp_blog');
  7. $lightphp['db'] = new lightphp_mysqldb($dbconfig['host'],$dbconfig['port'],$dbconfig['usr'],$dbconfig['pwd'],$dbconfig['dbname']);
  8. $lightphp['db']->query('set names utf8');

一个便于维护的项目,必然是经过模块代码封装的项目,所以我们的数据层操作放在动作代码里就不合适了,由此我们在lib/下创建article文件夹,创建一个用于存放article模块用到的数据操作方法articledb.php。
定义默认首页,在config/custom.config.php中继续加入:

  1. $lightphp['defaultmodule'] = 'article';//重新定义默认模块
  2. $lightphp['defaultaction'] = 'listall';//重新定义默认动作

IV.编写代码

/lib/article/articledb.php:

  1. <?php
  2. function listalldb(){
  3.   global $lightphp;
  4.  
  5.   $currentPage = ($_GET['page'])?intval($_GET['page']):1;
  6.  
  7.   $sql = "select count(*) as totalCount from lightphp_article";
  8.   $query = $lightphp['db']->query($sql);
  9.   $rs = $lightphp['db']->fetch_assoc($query);
  10.   $totalCount = $rs[0]['totalCount'];
  11.  
  12.   if ($totalCount > 0){
  13.     $numPerPage = 3;
  14.     if ($totalCount < $numPerPage){
  15.       $pageCount = 1;
  16.     } else {
  17.       $pageCount = ceil($totalCount / $numPerPage);
  18.     }
  19.     $offSet = ($currentPage - 1) * $numPerPage;
  20.     $sql = "select * from lightphp_article order by recordID desc limit {$offSet},{$numPerPage}";
  21.     $query = $lightphp['db']->query($sql);
  22.     $articles = $lightphp['db']->fetch_assoc($query);
  23.     foreach ($articles as $key => $value){
  24.       $articles[$key]['createDate'] = date('Y-m-d H:i:s',$value['createDate']);
  25.       $sql = "select categoryName from lightphp_category where recordID = {$value['categoryID']}";
  26.       $query = $lightphp['db']->query($sql);
  27.       $rs = $lightphp['db']->fetch_assoc($query);
  28.       $articles[$key]['categoryName'] = $rs[0]['categoryName'];
  29.     }
  30.    
  31.     return array('totalCount' => $totalCount,
  32.                  'pageCount' => $pageCount,
  33.                  'articles' => $articles,
  34.                  'currentPage' => $currentPage);
  35.   } else {
  36.     return false;
  37.   }
  38. }

/module/article/listall.php:

  1. <?php
  2. function listall(){
  3.   load_lib('article/articledb');
  4.   $articles = listalldb();
  5.   $data = array('articles' => $articles);
  6.   return template('article/listall',$data);
  7. }

/view/article/listall.php

  1. <?php template('head'); ?>
  2.       <div id="page-bgcontent">
  3.               <div id="content">
  4.           <h3>文章列表</h3>
  5.                   <div class="post">
  6.             <?php if ($articles): ?>
  7.               <?php foreach ($articles['articles'] as $key => $value): ?>
  8.                           <h2 class="title"><a href="/show/<?php echo $value['recordID']; ?>"><?php echo $value['title']; ?></a>[<?php echo $value['createDate']; ?>]</h2>
  9.                           <div class="entry">
  10.                   <?php echo $value['body']; ?>
  11.                           </div>
  12.                 <div>分类:<?php echo $value['categoryName']; ?></div>
  13.               <?php endforeach; ?>
  14.               <br />
  15.               <?php
  16.                 load_lib('page.class');
  17.                 $page = new page();
  18.                 $page->totalCount = $articles['totalCount'];
  19.                 $page->numPerPage = 3;
  20.                 $page->currentPage = $articles['currentPage'];
  21.                 $page->pageUrl = '/listall';
  22.                 echo $page->generatePage();
  23.               ?>
  24.             <?php else: ?>
  25.             <h2 class="title">还没有文章</h2>
  26.             <?php endif; ?>
  27.             <a href="/add">发表文章</a>
  28.                   </div>
  29.               </div>
  30.               <div style="clear: both;">&nbsp;</div>
  31.           </div>
  32.     <?php template('foot'); ?>

4.总结

具体可以下载源代码进行查看,ligthphp是轻型php开发框架,按需定制,最大限度提高php程序性能。
下载源代码:

zip

tar.gz

Post a Comment