1.打開后臺入口文件admin.php
header('location:index.php?m=admin');
跳轉(zhuǎn)到index.php并且m=admin
2.打開index.php
define('PHPCMS_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR); include PHPCMS_PATH.'/phpcms/base.php'; pc_base::creat_app();
定義了根目錄,包含了框架的入口文件base.php,并且使用類靜態(tài)方法creat_app()
3.打開框架入口文件base.php
define('IN_PHPCMS', true); //PHPCMS框架路徑 define('PC_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR); if(!defined('PHPCMS_PATH')) define('PHPCMS_PATH', PC_PATH.'..'.DIRECTORY_SEPARATOR); //緩存文件夾地址 define('CACHE_PATH', PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR); //主機(jī)協(xié)議 define('SITE_PROTOCOL', isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://'); //當(dāng)前訪問的主機(jī)名 define('SITE_URL', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '')); //來源 define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''); //系統(tǒng)開始時(shí)間 define('SYS_START_TIME', microtime()); //加載公用函數(shù)庫 pc_base::load_sys_func('global'); pc_base::load_sys_func('extention'); pc_base::auto_load_func(); pc_base::load_config('system','errorlog') ? set_error_handler('my_error_handler') : error_reporting(E_ERROR | E_WARNING | E_PARSE); //設(shè)置本地時(shí)差 function_exists('date_default_timezone_set') && date_default_timezone_set(pc_base::load_config('system','timezone')); define('CHARSET' ,pc_base::load_config('system','charset')); //輸出頁面字符集 header('Content-type: text/html; charset='.CHARSET); define('SYS_TIME', time()); //定義網(wǎng)站根路徑 define('WEB_PATH',pc_base::load_config('system','web_path')); //js 路徑 define('JS_PATH',pc_base::load_config('system','js_path')); //css 路徑 define('CSS_PATH',pc_base::load_config('system','css_path')); //img 路徑 define('IMG_PATH',pc_base::load_config('system','img_path')); //動態(tài)程序路徑 define('APP_PATH',pc_base::load_config('system','app_path')); //應(yīng)用靜態(tài)文件路徑 define('PLUGIN_STATICS_PATH',WEB_PATH.'statics/plugin/'); if(pc_base::load_config('system','gzip') && function_exists('ob_gzhandler')) { ob_start('ob_gzhandler'); } else { ob_start(); } class pc_base { /** * 初始化應(yīng)用程序 */ public static function creat_app() { return self::load_sys_class('application'); } /** * 加載系統(tǒng)類方法 * @param string $classname 類名 * @param string $path 擴(kuò)展地址 * @param intger $initialize 是否初始化 */ public static function load_sys_class($classname, $path = '', $initialize = 1) { return self::_load_class($classname, $path, $initialize); } /** * 加載應(yīng)用類方法 * @param string $classname 類名 * @param string $m 模塊 * @param intger $initialize 是否初始化 */ public static function load_app_class($classname, $m = '', $initialize = 1) { $m = empty($m) && defined('ROUTE_M') ? ROUTE_M : $m; if (empty($m)) return false; return self::_load_class($classname, 'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'classes', $initialize); } /** * 加載數(shù)據(jù)模型 * @param string $classname 類名 */ public static function load_model($classname) { return self::_load_class($classname,'model'); } /** * 加載類文件函數(shù) * @param string $classname 類名 * @param string $path 擴(kuò)展地址 * @param intger $initialize 是否初始化 */ private static function _load_class($classname, $path = '', $initialize = 1) { static $classes = array(); if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'classes'; $key = md5($path.$classname); if (isset($classes[$key])) { if (!empty($classes[$key])) { return $classes[$key]; } else { return true; } } if (file_exists(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) { include PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php'; $name = $classname; if ($my_path = self::my_path(PC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) { include $my_path; $name = 'MY_'.$classname; } if ($initialize) { $classes[$key] = new $name; } else { $classes[$key] = true; } return $classes[$key]; } else { return false; } } /** * 加載系統(tǒng)的函數(shù)庫 * @param string $func 函數(shù)庫名 */ public static function load_sys_func($func) { return self::_load_func($func); } /** * 自動加載autoload目錄下函數(shù)庫 * @param string $func 函數(shù)庫名 */ public static function auto_load_func($path='') { return self::_auto_load_func($path); } /** * 加載應(yīng)用函數(shù)庫 * @param string $func 函數(shù)庫名 * @param string $m 模型名 */ public static function load_app_func($func, $m = '') { $m = empty($m) && defined('ROUTE_M') ? ROUTE_M : $m; if (empty($m)) return false; return self::_load_func($func, 'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'functions'); } /** * 加載插件類庫 */ public static function load_plugin_class($classname, $identification = '' ,$initialize = 1) { $identification = empty($identification) && defined('PLUGIN_ID') ? PLUGIN_ID : $identification; if (empty($identification)) return false; return pc_base::load_sys_class($classname, 'plugin'.DIRECTORY_SEPARATOR.$identification.DIRECTORY_SEPARATOR.'classes', $initialize); } /** * 加載插件函數(shù)庫 * @param string $func 函數(shù)文件名稱 * @param string $identification 插件標(biāo)識 */ public static function load_plugin_func($func,$identification) { static $funcs = array(); $identification = empty($identification) && defined('PLUGIN_ID') ? PLUGIN_ID : $identification; if (empty($identification)) return false; $path = 'plugin'.DIRECTORY_SEPARATOR.$identification.DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.$func.'.func.php'; $key = md5($path); if (isset($funcs[$key])) return true; if (file_exists(PC_PATH.$path)) { include PC_PATH.$path; } else { $funcs[$key] = false; return false; } $funcs[$key] = true; return true; } /** * 加載插件數(shù)據(jù)模型 * @param string $classname 類名 */ public static function load_plugin_model($classname,$identification) { $identification = empty($identification) && defined('PLUGIN_ID') ? PLUGIN_ID : $identification; $path = 'plugin'.DIRECTORY_SEPARATOR.$identification.DIRECTORY_SEPARATOR.'model'; return self::_load_class($classname,$path); } /** * 加載函數(shù)庫 * @param string $func 函數(shù)庫名 * @param string $path 地址 */ private static function _load_func($func, $path = '') { static $funcs = array(); if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions';//默認(rèn)函數(shù)地址在lib function $path .= DIRECTORY_SEPARATOR.$func.'.func.php'; $key = md5($path); if (isset($funcs[$key])) return true; if (file_exists(PC_PATH.$path)) { include PC_PATH.$path; } else { $funcs[$key] = false; return false; } $funcs[$key] = true; return true; } /** * 加載函數(shù)庫 * @param string $func 函數(shù)庫名 * @param string $path 地址 */ private static function _auto_load_func($path = '') { if (empty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions'.DIRECTORY_SEPARATOR.'autoload'; $path .= DIRECTORY_SEPARATOR.'*.func.php'; $auto_funcs = glob(PC_PATH.DIRECTORY_SEPARATOR.$path); if(!empty($auto_funcs) && is_array($auto_funcs)) { foreach($auto_funcs as $func_path) { include $func_path; } } } /** * 是否有自己的擴(kuò)展文件 * @param string $filepath 路徑 */ public static function my_path($filepath) { $path = pathinfo($filepath); if (file_exists($path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename'])) { return $path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename']; } else { return false; } } /** * 加載配置文件 * @param string $file 配置文件 * @param string $key 要獲取的配置薦 * @param string $default 默認(rèn)配置。當(dāng)獲取配置項(xiàng)目失敗時(shí)該值發(fā)生作用。 * @param boolean $reload 強(qiáng)制重新加載。 */ public static function load_config($file, $key = '', $default = '', $reload = false) { static $configs = array(); if (!$reload && isset($configs[$file])) { if (empty($key)) { return $configs[$file]; } elseif (isset($configs[$file][$key])) { return $configs[$file][$key]; } else { return $default; } } $path = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.$file.'.php'; if (file_exists($path)) { $configs[$file] = include $path; } if (empty($key)) { return $configs[$file]; } elseif (isset($configs[$file][$key])) { return $configs[$file][$key]; } else { return $default; } } }
①定義各后期可能用到的常量
②創(chuàng)建了pc_base類,加載類并且實(shí)例化和
③加載基本框架需要的函數(shù)庫
④定義creat_app() 包含application且實(shí)例化
4.打開框架類庫文件下的application.class.php文件
執(zhí)行構(gòu)造函數(shù)
加載param路由類,使用路由類中 的$param->route_m(),$param->route_c(),$param->route_a()獲取模塊和控制器以 及方法,route_m為模塊,在modules文件下,然后是控制器和方法,標(biāo)準(zhǔn)的mvc結(jié)構(gòu)
然后執(zhí)行int函數(shù)
執(zhí)行l(wèi)oad_controller加載獲取到的控制器并且實(shí)例化!
5.打開index.php?m=admin&a=index&c=login登錄頁面
defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_app_class('admin','admin',0);
判斷是否從框架入口文件進(jìn)入,然后加載admin模塊下的admin類文件
class index extends admin { public function __construct() { parent::__construct(); //調(diào)用父類構(gòu)造方法 $this->db = pc_base::load_model('admin_model'); $this->menu_db = pc_base::load_model('menu_model'); $this->panel_db = pc_base::load_model('admin_panel_model'); }
然后使用構(gòu)造方法加載使用到的模型文件(MVC中的M),然后看父類的構(gòu)造方法
6.打開框架類文件admin.class.php
這個(gè)類文件,這個(gè)模塊中的最高級類,做了很多判斷操作,以及本模塊一些必須的東西(加載模板)
7.然后讓我們看admin_model.class.php類
defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_sys_class('model', '', 0); class admin_model extends model { public function __construct() { $this->db_config = pc_base::load_config('database'); $this->db_setting = 'default'; $this->table_name = 'admin'; parent::__construct(); } }
admin_model extends model繼承model然后使用了一些方法
<?php /** * model.class.php 數(shù)據(jù)模型基類 * * @copyright (C) 2005-2010 PHPCMS * @license http://www.phpcms.cn/license/ * @lastmodify 2010-6-7 */ defined('IN_PHPCMS') or exit('Access Denied'); pc_base::load_sys_class('db_factory', '', 0); class model { //數(shù)據(jù)庫配置 protected $db_config = ''; //數(shù)據(jù)庫連接 protected $db = ''; //調(diào)用數(shù)據(jù)庫的配置項(xiàng) protected $db_setting = 'default'; //數(shù)據(jù)表名 protected $table_name = ''; //表前綴 public $db_tablepre = ''; public function __construct() { if (!isset($this->db_config[$this->db_setting])) { $this->db_setting = 'default'; } $this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name; $this->db_tablepre = $this->db_config[$this->db_setting]['tablepre']; $this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting); } /** * 執(zhí)行sql查詢 * @param $where 查詢條件[例`name`='$name'] * @param $data 需要查詢的字段值[例`name`,`gender`,`birthday`] * @param $limit 返回結(jié)果范圍[例:10或10,10 默認(rèn)為空] * @param $order 排序方式 [默認(rèn)按數(shù)據(jù)庫默認(rèn)方式排序] * @param $group 分組方式 [默認(rèn)為空] * @param $key 返回?cái)?shù)組按鍵名排序 * @return array 查詢結(jié)果集數(shù)組 */ final public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') { if (is_array($where)) $where = $this->sqls($where); return $this->db->select($data, $this->table_name, $where, $limit, $order, $group, $key); } /** * 查詢多條數(shù)據(jù)并分頁 * @param $where * @param $order * @param $page * @param $pagesize * @return unknown_type */ final public function listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*') { $where = to_sqls($where); $this->number = $this->count($where); $page = max(intval($page), 1); $offset = $pagesize*($page-1); $this->pages = pages($this->number, $page, $pagesize, $urlrule, $array, $setpages); $array = array(); if ($this->number > 0) { return $this->select($where, $data, "$offset, $pagesize", $order, '', $key); } else { return array(); } } /** * 獲取單條記錄查詢 * @param $where 查詢條件 * @param $data 需要查詢的字段值[例`name`,`gender`,`birthday`] * @param $order 排序方式 [默認(rèn)按數(shù)據(jù)庫默認(rèn)方式排序] * @param $group 分組方式 [默認(rèn)為空] * @return array/null 數(shù)據(jù)查詢結(jié)果集,如果不存在,則返回空 */ final public function get_one($where = '', $data = '*', $order = '', $group = '') { if (is_array($where)) $where = $this->sqls($where); return $this->db->get_one($data, $this->table_name, $where, $order, $group); } /** * 直接執(zhí)行sql查詢 * @param $sql 查詢sql語句 * @return boolean/query resource 如果為查詢語句,返回資源句柄,否則返回true/false */ final public function query($sql) { $sql = str_replace('phpcms_', $this->db_tablepre, $sql); return $this->db->query($sql); } /** * 執(zhí)行添加記錄操作 * @param $data 要增加的數(shù)據(jù),參數(shù)為數(shù)組。數(shù)組key為字段值,數(shù)組值為數(shù)據(jù)取值 * @param $return_insert_id 是否返回新建ID號 * @param $replace 是否采用 replace into的方式添加數(shù)據(jù) * @return boolean */ final public function insert($data, $return_insert_id = false, $replace = false) { return $this->db->insert($data, $this->table_name, $return_insert_id, $replace); } /** * 獲取最后一次添加記錄的主鍵號 * @return int */ final public function insert_id() { return $this->db->insert_id(); } /** * 執(zhí)行更新記錄操作 * @param $data 要更新的數(shù)據(jù)內(nèi)容,參數(shù)可以為數(shù)組也可以為字符串,建議數(shù)組。 * 為數(shù)組時(shí)數(shù)組key為字段值,數(shù)組值為數(shù)據(jù)取值 * 為字符串時(shí)[例:`name`='phpcms',`hits`=`hits`+1]。 * 為數(shù)組時(shí)[例: array('name'=>'phpcms','password'=>'123456')] * 數(shù)組的另一種使用array('name'=>'+=1', 'base'=>'-=1');程序會自動解析為`name` = `name` + 1, `base` = `base` - 1 * @param $where 更新數(shù)據(jù)時(shí)的條件,可為數(shù)組或字符串 * @return boolean */ final public function update($data, $where = '') { if (is_array($where)) $where = $this->sqls($where); return $this->db->update($data, $this->table_name, $where); } /** * 執(zhí)行刪除記錄操作 * @param $where 刪除數(shù)據(jù)條件,不充許為空。 * @return boolean */ final public function delete($where) { if (is_array($where)) $where = $this->sqls($where); return $this->db->delete($this->table_name, $where); } /** * 計(jì)算記錄數(shù) * @param string/array $where 查詢條件 */ final public function count($where = '') { $r = $this->get_one($where, "COUNT(*) AS num"); return $r['num']; } /** * 將數(shù)組轉(zhuǎn)換為SQL語句 * @param array $where 要生成的數(shù)組 * @param string $font 連接串。 */ final public function sqls($where, $font = ' AND ') { if (is_array($where)) { $sql = ''; foreach ($where as $key=>$val) { $sql .= $sql ? " $font `$key` = '$val' " : " `$key` = '$val'"; } return $sql; } else { return $where; } } /** * 獲取最后數(shù)據(jù)庫操作影響到的條數(shù) * @return int */ final public function affected_rows() { return $this->db->affected_rows(); } /** * 獲取數(shù)據(jù)表主鍵 * @return array */ final public function get_primary() { return $this->db->get_primary($this->table_name); } /** * 獲取表字段 * @param string $table_name 表名 * @return array */ final public function get_fields($table_name = '') { if (empty($table_name)) { $table_name = $this->table_name; } else { $table_name = $this->db_tablepre.$table_name; } return $this->db->get_fields($table_name); } /** * 檢查表是否存在 * @param $table 表名 * @return boolean */ final public function table_exists($table){ return $this->db->table_exists($this->db_tablepre.$table); } /** * 檢查字段是否存在 * @param $field 字段名 * @return boolean */ public function field_exists($field) { $fields = $this->db->get_fields($this->table_name); return array_key_exists($field, $fields); } final public function list_tables() { return $this->db->list_tables(); } /** * 返回?cái)?shù)據(jù)結(jié)果集 * @param $query (mysql_query返回值) * @return array */ final public function fetch_array() { $data = array(); while($r = $this->db->fetch_next()) { $data[] = $r; } return $data; } /** * 返回?cái)?shù)據(jù)庫版本號 */ final public function version() { return $this->db->version(); } }
包含了數(shù)據(jù)庫操作封裝,然后數(shù)據(jù)庫的連接,大致思路就是如此