easyphp 框架简介
fanglor 2011-05-26
1.easyphp 是什么?
easyphp 是一个简单实现了MVC模式、统一入口及常用函数集成的易用的php框架
2.文件结构
————————–
│ admin.php
│ index.php
│
├─admin //后台项目文件夹
│ ├─controller
│ ├─model
│ └─view
├─app //前台项目文件夹
│ │ index.php //默认空白文件
│ │
│ ├─controller
│ ├─model
│ │
│ └─view
│ └─public
├─cache //缓存文件夹
│ ├─data //数据缓存文件夹
│ └─template //模板编译文件夹
│
├─core //模板核心文件夹
│ │ base_controller.class.php //controller层基类,里面集成了实例化的mod和view ,以及框架起动函数
│ │ base_model.class.php //model层基类,继承于mysql类
│ │ base_view.class.php //view层基类,内部实例化了smarty
│ │ config.inc.php //数据库配置文件
│ │ easyphp.php //公共包含文件,内引进了框架启动所必需的文件
│ │ global.func.php //公共函数文件
│ │ image.class.php //图片处理类
│ │ message.html //默认提示信息模板
│ │ mysql.class.php //mysql数据库类
│ │
│ ├─font //字体文件夹
│ │
│ └─smarty //集成smarty模板文件夹
│
├─d3f //第三方扩展文件夹
└─doc //文档文件夹
└─sql //数据库文件夹
3. 常用集成函数
function C ($ctl) //快速创建控制器对象
function D () //快速创建模型对象,支持空模型
function M ($mod) //快速创建模型对象,不支持空模型
function __autoload ($classname) //模型自动加载类
function addslashes_deep($value) //递归转义字符串
function del_cache () //删除缓存文件
function get_child_ids ($sid = 0) //得到子分类id集
function get_sort ($table , $pid = 0 ,$n=-1) //无限分类函数
function get_sort_path ($url ,$sort_id) //得到分类全路径函数(类似面包屑式导航)
function go_to_url ($url=”) //JS实现无提示直接跳转
function msg ($msg ,$url = ”) //JS提示框
function page ($total,$url,$pagesize,$_var=’p',$str=”) //通用分页函数
function rand_str ( $len = 4) //随机生成字符串函数
function read_file ($key ) //读取文件缓存
function rmkdir ($path) //创建多层文件目录函数
function truncate_cn($string, $length = 80, $etc = ‘…’, $code = ‘gbk’)//中文字符串截取
function upload ( $file ,$upload_dir= ‘.’ ,$max_size = 500) //文件上传函数
function url ($c,$a,$args =array ()) //url 构造函数
function vcode ($index=’vcode’) //验证码函数,会用到core/font里的文件
function write_file ($key ,$val ,$lifttime=3600) //写文件缓存函数
function xmd5 ($str) //加强MD5加密函数
include “mod/Message.class.php”;
$message =new MWT_Message();
$temp2 = $message->fields(‘id,title,content’)->where( ‘ name = “123″ or sex like “%body”‘)->order(‘id asc ‘)->limit(’3,5′)->findAll ();
var_dump ($temp2);
exit;
4. MVC
4.1 mod
model层必须继承 base_model类 ,在里面设好 $table (表名 ,##__表示表前缀,在config.inc.php中定义) ,$pk (主键),保存在各项目文件夹中的model内,文件名必须为xxx_model.class.php ,里面的类名为去掉_model.class.php 之后的名字
4.2 view
模板文件保存在各项目文件夹中的view内,文件名无限制,但一般与 controller的类名一致,后缀名为.html 。<% %> 为定义的模板标签定界符 ,变量应写在其中,例如 <%$username%> ,在 controller类的方法中 可以用 形如 $this->username = ‘fanglor’; 或 $this->assign (‘username’,'fanglor’);把变量赋值到模板,最后 用 $this->display (‘index.html’); 方法调用并显示模板内容
4.3 controller
controller层必须继承 base_controller类 ,在里面最上面一行写上 !defined (‘IN_EASY’) && die (‘没有权限 ‘.basename(__file__)); 权限校验
,保存在各项目文件夹中的controller内,文件名必须为xxx_controller.class.php ,里面的类名为去掉_controller.class.php 之后的名字
4.4 mvc的实现
主要是通过地址路由转发的 ,例如: index.php?c=message&a=list 会自动调用 message_controller.class.php 中的 list方法 ,所以controller类中的各个方法一般对应一个地址动作
5,数据库常用 操作 方法 例举
增加:
M(‘message’)->insert ($data ); //添加 $data 为 要插入的数据 ,索引为 映射好的 索引名
删除 :
M(‘message’)->del () // 全部删除
M(‘message’)->where (‘id < 5′)->del (); //删除ID<5的记录
M(‘message’)->delet e (5) ; // 删除 主键为 5的记录,只适用于删除当前主键为该数字的情况下
M(‘message’)->delet e (); //删除所有
修改:
M(‘message’)->update ($data); //全部修改
M(‘message’)->where (” id = 7″ )->update ($data); //修改ID = 7 的记录 为 $data 里的内容,$data为数组,索引为映射后的
查询 :
M(‘message’)->find_by_id ($id); //查一条记录的所有字段 (一维数组) ,结果同下
M(‘message’)->where (” id = ‘$id’” )->find ();
M(‘message’)->fields (‘title,content’)->where (‘id = 7′)->find (); //查询一条记录的某些字段 (一维数组)
M(‘message’)->fields (‘content’) ->findAll() //查表中的所有数据 (二维数组)
M(‘message’)->where (“id > 7″)-> findAll();
M(‘message’)->fields (‘user_1′) ->where (“id > 7″)->order(‘id desc ‘)-> findAll();
M(‘message’)->fields (‘user_1′) ->where (“id > 7″)->order(‘id_1 desc ‘)->limit(10,5)-> findAll(); //用于分页
多表查询 :
M(‘message’)->fields(“a.uid,a.title,a.content,b.username”)->table(“##__message a left join ##__user b on a.uid = b.uid “)->findAll();
其它:
result_sql :执行SQL返回多维数据的方法 (适用于select语句)
$this->message_list = M(‘message’)->resultSql(“SELECT m.*,u.username fro m message m LEFT JOIN userinfo u ON m.user_id = u.user_id WHERE 1 ORDER BY m.mid “);
exec_sql : 直接执行SQL方法 (适用于insert ,update ,delet e )
$this->exec_sql (” delet e fro m ##__message where id = ‘{$id}’”);
count :查询记录条数方法
$total = M(‘message’)->count ();//查出表中共有多少条记录
$total = M(‘message’)->where (” name = ‘fanglor’”)->count ();//查出表中叫fanglor 的共有多少条记录
get_field :获取某个字段的值 ,返回值为字段串
$username = M(‘user’)->fields(‘username’)->where (” id = 5″)->get_field ();//查出id为5的用户的名字
6.快速入门例子
详见 演示代码中的留言本例子
