PHP框架 LazyPHP

fmms 12年前

LazyPHP 是一个简单的PHP框架.

整合了 Yui css,Mootools,Simple Test和JsUnit等组件和工具,适合于项目启动时作为初始框架使用.

说明文档

访问流程

所有的请求都将通过/index.php进行转发.在请求数据中,变量m和变量a标示了该请求会转发到的class和method.

未指明时,LP自动加载/core/config/core.config.php中的设置.以下代码说明了请求转发的过程.

$post_fix = '.class.php';  $mod_file = AROOT . 'mod' . DS . $m . $post_fix;    if( !class_exists( $m.'Mod' ) ) die('Can\'t find class - ' . $m . 'Mod');  $class_name =$m.'Mod';  $o = new $class_name;  if( !method_exists( $o , $a ) ) die('Can\'t find method - ' . $a . ' ');    call_user_method( $a , $o );

由以上代码可知,每次请求,LP都会执行 /app/mod/目录下的某个class的实例的方法. 通常我们会将一组功能放置到一个mod文件中,如user.class.php,包含了 reg,save,modify,update,login,logout等方法.

以User的modify为例,我们处理的过程为:

在/app/mod/下,建立user.class.php文件,extend app.class.php:

if( !defined('IN') ) die('bad request');  include_once( AROOT . 'mod/app.class.php' );  class userMod extends appMod  {  function __construct()  {  parent::__construct();  }  }

然后,我们添加modify方法.

public function modify()  {  $id = intval(t(v('id')));  $data['title'] = $data['top_title'] = '修改资料';  $data['user'] = get_line("SELECT * FROM `user` WHERE `id` = " . s($id) . " LIMIT 1 ");  render( $data );  }

解释下以上代码. 首先通过v函数获取到了$REQUEST['id'],并通过t函数进行trim.v和c都是对常用函数的快捷封装,详细定义在/core/function/core.function.php中.

get_line函数是LP框架的数据库操作函数,用于取回sql对应的单行数据.数据库相关函数,在/core/function/db.function.php中定义.

render 函数是LP框架的模板系统核心函数,其实现却非常简单.将传入参数extract,然后载入模板.这里并没有指定对应的模板的文件,而是按约定为/app /view/layout/default/main/modify.tpl.html和/app/view/layout/default/side /modify.tpl.html.你可以修改/app/view/layout/default/index.tpl.html来自定义这个 layout的布局和模板存放目录的结构.

常用函数

LP的常用函数不超过20个,其中很多都是现有函数的缩写.

标准库函数

  • function v( $str ) // 取得 $REQUEST[$str] 的数据
  • function z( $str ) // strip_tags
  • function c( $str ) // 读取配置文件中$str为key的对应的value
  • function g( $str ) // 取得 $GLOBALS[$str] 的数据
  • function t( $str ) // trim
  • function render( $data = NULL , $layout = 'default' ) // 用$data渲染模板$layout
  • function info_page( $info , $layout = 'default' ) // 错误提示信息
  • function load( $file_path ) // 载入文件

数据库函数

  • function s( $str , $db = NULL ) // mysql_real_escape_string
  • function db() // 使用config目录下的数据库设置,创建并返回数据库链接
  • function get_data( $sql , $db = NULL ) // 以二维数组的方式返回$sql对应的结果
  • function get_line( $sql , $db = NULL ) // 以一维数组的方式返回$sql对应的单行结果
  • function get_var( $sql , $db = NULL ) // 以变量的方式返回一个数值
  • function last_id( $db = NULL ) // last id
  • function run_sql( $sql , $db = NULL ) // 运行sql,不返回结果集
  • function db_error() // 数据库错误信息
  • function db_errno() // 数据库错误编号
  • function close_db( $db = NULL ) // 显式关闭数据库链接

LP4SAE补充函数

  • send_mail( $email , $subject , $content );
  • file_get_url( $file_path ); // 返回通过http协议访问该文件的url

单元测试

访问/test.php即可对现有框架进行单元测试.在/test/phptest/下添加xxx.test.php文件,即可增加测试.

项目主页:http://www.open-open.com/lib/view/home/1329804133890