[lightphp入门教程]使用lightphp框架开发小型blog系统
October 7, 2009 – 6:33 pmlightphp适合熟练的phper进行企业级应用开发,如果单纯论及代码自动生成,ORM模型和yml载入之类功能,lightphp无法跟一些成熟的框架(例如symfony)相比。我们的目的是,按需定制,性能优先。
如果做访问量不高且功能简单的应用,lightphp就不是明智的选择,建议使用成熟的框架和数据操作方案,例如:symfony + Doctrine。但是一个入门教程又不可能写得太复杂,所以还是用构建小型blog应用做例子,麻雀虽小,但五脏俱全,基本涵盖了lightphp的开发流程,希望起到抛砖引玉的作用。
(本教程基于linux + apache + mysql + php平台)
1.下载安装lightphp
tar.gz
解压缩lightphp1.0.zip到你的web目录,配置虚拟主机为:
- <VirtualHost *:80>
- DocumentRoot "/home/htdocs/framework/lightphp/blog/0.2/control"
- ServerName blog.lightphp.com
- DirectoryIndex index.php
- <Directory "/home/htdocs/framework/lightphp/blog/0.2/control">
- Options Indexes FollowSymLinks
- AllowOverride All
- Order deny,allow
- Allow from all
- </Directory>
- </VirtualHost>
修改/etc/hosts,加入:
- 127.0.0.1 blog.lightphp.com
重新启动网络和apache服务器,在浏览器里输入http://blog.lightphp.com既可以访问到lightphp的欢迎界面。
2.blog数据库设计
数据库名就叫:lightphp_blog,由于功能实现很简单,所以我们只需要两个表即可实现。
文章表
- lightphp_article{
- recordID:(int(8))
- title:(varchar(100))
- body:(text)
- createDate:(int(10))
- updateDate(int(10))
- categoryID:(int(10))
- }
标签表
- lightphp_category{
- recordID:(int(8))
- categoryName:(varchar(100))
- }
3.开始写代码
I.明确功能需求,划分模块
由于这是一个小型blog系统,所以功能会变得很简单。
*选择分类发表文章;
*显示所有文章
*编辑文章
*删除文章
II.定义url重写规则(如果你不在乎index.php/module=xxx&action=xxx之类不友好的url,此部分可以略过)
打开control/.htaccess文件添加url重写规则如下:
- #添加文章
- RewriteRule ^add index.php?module=article&action=add
- #编辑文章
- RewriteRule ^edit/(\d+) index.php?module=article&action=edit&articleID=$1
- #删除文章
- RewriteRule ^del/(\d+) index.php?module=article&action=del&articleID=$1
- #显示所有文章
- RewriteRule ^listall/(\d+) index.php?module=article&action=listall&page=$1
- RewriteRule ^listall index.php?module=article&action=listall
- #查看单个文章
- 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中加入以下配置:
- $lightphp['modules']['article'] = array('listall',
- 'add',
- 'edit',
- 'del',
- 'show');
项目使用mysql数据库,为了后面的扩展性,我们封装一个mysql数据操作类。
在config/custom.config.php中继续加入数据库连接代码:
- load_lib('mysql.db.class');
- $dbconfig = array('host' => 'localhost',
- 'port' => '',
- 'usr' => 'root',
- 'pwd' => '',
- 'dbname' => 'lightphp_blog');
- $lightphp['db'] = new lightphp_mysqldb($dbconfig['host'],$dbconfig['port'],$dbconfig['usr'],$dbconfig['pwd'],$dbconfig['dbname']);
- $lightphp['db']->query('set names utf8');
一个便于维护的项目,必然是经过模块代码封装的项目,所以我们的数据层操作放在动作代码里就不合适了,由此我们在lib/下创建article文件夹,创建一个用于存放article模块用到的数据操作方法articledb.php。
定义默认首页,在config/custom.config.php中继续加入:
- $lightphp['defaultmodule'] = 'article';//重新定义默认模块
- $lightphp['defaultaction'] = 'listall';//重新定义默认动作
IV.编写代码
/lib/article/articledb.php:
- <?php
- function listalldb(){
- global $lightphp;
- $currentPage = ($_GET['page'])?intval($_GET['page']):1;
- $sql = "select count(*) as totalCount from lightphp_article";
- $query = $lightphp['db']->query($sql);
- $rs = $lightphp['db']->fetch_assoc($query);
- $totalCount = $rs[0]['totalCount'];
- if ($totalCount > 0){
- $numPerPage = 3;
- if ($totalCount < $numPerPage){
- $pageCount = 1;
- } else {
- $pageCount = ceil($totalCount / $numPerPage);
- }
- $offSet = ($currentPage - 1) * $numPerPage;
- $sql = "select * from lightphp_article order by recordID desc limit {$offSet},{$numPerPage}";
- $query = $lightphp['db']->query($sql);
- $articles = $lightphp['db']->fetch_assoc($query);
- foreach ($articles as $key => $value){
- $articles[$key]['createDate'] = date('Y-m-d H:i:s',$value['createDate']);
- $sql = "select categoryName from lightphp_category where recordID = {$value['categoryID']}";
- $query = $lightphp['db']->query($sql);
- $rs = $lightphp['db']->fetch_assoc($query);
- $articles[$key]['categoryName'] = $rs[0]['categoryName'];
- }
- return array('totalCount' => $totalCount,
- 'pageCount' => $pageCount,
- 'articles' => $articles,
- 'currentPage' => $currentPage);
- } else {
- return false;
- }
- }
/module/article/listall.php:
- <?php
- function listall(){
- load_lib('article/articledb');
- $articles = listalldb();
- $data = array('articles' => $articles);
- return template('article/listall',$data);
- }
/view/article/listall.php
- <?php template('head'); ?>
- <div id="page-bgcontent">
- <div id="content">
- <h3>文章列表</h3>
- <div class="post">
- <?php if ($articles): ?>
- <?php foreach ($articles['articles'] as $key => $value): ?>
- <h2 class="title"><a href="/show/<?php echo $value['recordID']; ?>"><?php echo $value['title']; ?></a>[<?php echo $value['createDate']; ?>]</h2>
- <div class="entry">
- <?php echo $value['body']; ?>
- </div>
- <div>分类:<?php echo $value['categoryName']; ?></div>
- <?php endforeach; ?>
- <br />
- <?php
- load_lib('page.class');
- $page = new page();
- $page->totalCount = $articles['totalCount'];
- $page->numPerPage = 3;
- $page->currentPage = $articles['currentPage'];
- $page->pageUrl = '/listall';
- echo $page->generatePage();
- ?>
- <?php else: ?>
- <h2 class="title">还没有文章</h2>
- <?php endif; ?>
- <a href="/add">发表文章</a>
- </div>
- </div>
- <div style="clear: both;"> </div>
- </div>
- <?php template('foot'); ?>
4.总结
具体可以下载源代码进行查看,ligthphp是轻型php开发框架,按需定制,最大限度提高php程序性能。
下载源代码: