PHP框架 LazyPHP
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文件,即可增加測試.