您现在的位置是:首页 > cms教程 > phpcms教程phpcms教程
PHPCMS源码引擎分析
含烟2025-05-21phpcms教程已有人查阅
导读如:调用单页面index.php?m=content&c=index&a=lists&catid=9.1.先获取到模版变量的值$template_list="list";然后通过$type!=0来判断是单页面,
一、模版引擎
如:调用单页面index.php?m=content&c=index&a=lists&catid=9.
1.先获取到模版变量的值$template_list="list";然后通过$type!=0来判断是单页面,
然后通过$template = $setting['page_template'] ? $setting['page_template'] : 'page';获取单页免的模版page.html.
2.引入模版include template('content',$template);
3.查看template()函数:定义在/phpcms/libs/functions/global.func.php.
(1)module;
(2)模版,比如单页面page.html;
(3)模版风格,就是/phpcms/templates/下面的目录名,比如default,che.
该函数实现的功能:
(1)通过file_exists($compiledtplfile):判断模版编译文件是否存在;
(2)通过filemtime()判断模版文件的生成时间是否大于模版编译文件;
以上两点如果(||连接)为真,则通过$template_cache->template_compile($module, $template, $style);编译模版文件;
4.查看template_compile()函数:定义在/phpcms/libs/classes/template_cache.class.php.
(同上)
该函数实现的功能:
(1)通过$content = @file_get_contents ( $tplfile );读取模版源文件的内容;
(2)通过$content = $this->template_parse($content);正则匹配替换掉标签,将标签替换为php代码:
(3)通过$strlen = file_put_contents ( $compiledtplfile, $content );将替换后的字符串写入模版编译文件;
(4)返回$strlen,即用include template('content',$template);将$strlen引入到控制器代码里。其本质就是php和html代码混编。
5.模版解析:通过template_parse($str)用正则替换标签。
2.\1 \2分组的使用;
3.替换为self::pc_tag函数的使用。
二、框架实现思路(参考:http:// .tuicool.com/articles/BvU3i2v)
通过单一入口的pc_base::create_app()来创建一个应用,调用不同的类库处理不同的应用;
当处理application时:调用框架类库文件下的application.class.php文件,执行构造函数,加载param路由类,使用路由类中的
$param->route_m(),$param->route_c(),$param->route_a()获取模块和控制器以及方法,route_m为模块,在modules文件下,然后是控制器和方法,标准的mvc结构。然后执行int函数,执行load_controller加载获取到的控制器并且实例化!
三、外部引用来调用phpcms的类库
1.三大类
mysql.class.php 数据库实现类 :final class mysql;
db_factory.class.php 数据库工厂类 :final class db_factory;
model.class.php 数据模型基类 :class model。
2.实现流程(/phpcms/libs/class/)
(1)mysql.class.php提供数据库连接、查询、执行等各种实现方法;
如:调用单页面index.php?m=content&c=index&a=lists&catid=9.
1.先获取到模版变量的值$template_list="list";然后通过$type!=0来判断是单页面,
然后通过$template = $setting['page_template'] ? $setting['page_template'] : 'page';获取单页免的模版page.html.
2.引入模版include template('content',$template);
3.查看template()函数:定义在/phpcms/libs/functions/global.func.php.
template($module = 'content', $template = 'index', $style = '');
传入的三个参数分别是:(1)module;
(2)模版,比如单页面page.html;
(3)模版风格,就是/phpcms/templates/下面的目录名,比如default,che.
该函数实现的功能:
(1)通过file_exists($compiledtplfile):判断模版编译文件是否存在;
(2)通过filemtime()判断模版文件的生成时间是否大于模版编译文件;
以上两点如果(||连接)为真,则通过$template_cache->template_compile($module, $template, $style);编译模版文件;
4.查看template_compile()函数:定义在/phpcms/libs/classes/template_cache.class.php.
template_compile($module, $template, $style = 'default');
传入的三个参数分别是:(同上)
该函数实现的功能:
(1)通过$content = @file_get_contents ( $tplfile );读取模版源文件的内容;
(2)通过$content = $this->template_parse($content);正则匹配替换掉标签,将标签替换为php代码:
(3)通过$strlen = file_put_contents ( $compiledtplfile, $content );将替换后的字符串写入模版编译文件;
(4)返回$strlen,即用include template('content',$template);将$strlen引入到控制器代码里。其本质就是php和html代码混编。
5.模版解析:通过template_parse($str)用正则替换标签。
/**
* 解析模板
*
* @param $str 模板内容
* @return ture
*/
public function template_parse($str) {
$str = preg_replace ( "/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $str );
$str = preg_replace ( "/\{include\s+(.+)\}/", "<?php include \\1; ?>", $str );
$str = preg_replace ( "/\{php\s+(.+)\}/", "<?php \\1?>", $str );
$str = preg_replace ( "/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str );
$str = preg_replace ( "/\{else\}/", "<?php } else { ?>", $str );
$str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str );
$str = preg_replace ( "/\{\/if\}/", "<?php } ?>", $str );
//for 循环
$str = preg_replace("/\{for\s+(.+?)\}/","<?php for(\\1) { ?>",$str);
$str = preg_replace("/\{\/for\}/","<?php } ?>",$str);
//++ --
$str = preg_replace("/\{\+\+(.+?)\}/","<?php ++\\1; ?>",$str);
$str = preg_replace("/\{\-\-(.+?)\}/","<?php ++\\1; ?>",$str);
$str = preg_replace("/\{(.+?)\+\+\}/","<?php \\1++; ?>",$str);
$str = preg_replace("/\{(.+?)\-\-\}/","<?php \\1--; ?>",$str);
$str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $str );
$str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $str );
$str = preg_replace ( "/\{\/loop\}/", "<?php \$n++;}unset(\$n); ?>", $str );
$str = preg_replace ( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
$str = preg_replace ( "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
$str = preg_replace ( "/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str );
$str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "\$this->addquote('<?php echo \\1;?>')",$str);
$str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $str );
$str = preg_replace("/\{pc:(\w+)\s+([^}]+)\}/ie", "self::pc_tag('$1','$2', '$0')", $str);
$str = preg_replace("/\{\/pc\}/ie", "self::end_pc_tag()", $str);
$str = "<?php defined('IN_PHPCMS') or exit('No permission resources.'); ?>" . $str;
return $str;
}
关键点:1.\s 空格匹配的使用;2.\1 \2分组的使用;
3.替换为self::pc_tag函数的使用。
二、框架实现思路(参考:http:// .tuicool.com/articles/BvU3i2v)
通过单一入口的pc_base::create_app()来创建一个应用,调用不同的类库处理不同的应用;
当处理application时:调用框架类库文件下的application.class.php文件,执行构造函数,加载param路由类,使用路由类中的
$param->route_m(),$param->route_c(),$param->route_a()获取模块和控制器以及方法,route_m为模块,在modules文件下,然后是控制器和方法,标准的mvc结构。然后执行int函数,执行load_controller加载获取到的控制器并且实例化!
三、外部引用来调用phpcms的类库
include_once '../phpcms/base.php';//引入主文件
pc_base::load_sys_class('model', '', 0);//加载model类
class Api extends model {//继承model类,即调用model的各种数据库方法
public function __construct() {
$this->db_config = include "../config/database.php";//此处得包含进数据库配置文件
$this->db_setting = 'default';
//$this->table_name = 'admin';
parent::__construct();
}
}
$apiDb = new Api();
$adpos = isset($_GET['adpos'])&&!empty($_GET['adpos'])?$_GET['adpos']:"0";
if($adpos=="login"){
$spaceid = 25;
}elseif ($adpos=="register"){
$spaceid = 23;
}else{
$spaceid = 0;
}
$sql = "SELECT s.width,s.height,p.setting FROM `u8_poster_space` s LEFT JOIN `u8_poster` p ON s.spaceid=p.spaceid WHERE p.spaceid=$spaceid";
$res = $apiDb->db->get_ones($sql);//根据sql语句获取一行结果集
四、数据库实现原理1.三大类
mysql.class.php 数据库实现类 :final class mysql;
db_factory.class.php 数据库工厂类 :final class db_factory;
model.class.php 数据模型基类 :class model。
2.实现流程(/phpcms/libs/class/)
(1)mysql.class.php提供数据库连接、查询、执行等各种实现方法;
/**
* 真正开启数据库连接
*
* @return void
*/
public function connect() {
$func = $this->config['pconnect'] == 1 ? 'mysql_pconnect' : 'mysql_connect';
if(!$this->link = @$func($this->config['hostname'], $this->config['username'], $this->config['password'], 1)) {
$this->halt('Can not connect to MySQL server');
return false;
}
if($this->version() > '4.1') {
$charset = isset($this->config['charset']) ? $this->config['charset'] : '';
$serverset = $charset ? "character_set_connection='$charset',character_set_results='$charset',character_set_client=binary" : '';
$serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',')." sql_mode='' ") : '';
$serverset && mysql_query("SET $serverset", $this->link);
}
if($this->config['database'] && !@mysql_select_db($this->config['database'], $this->link)) {
$this->halt('Cannot use database '.$this->config['database']);
return false;
}
$this->database = $this->config['database'];
return $this->link;
}
(2)db_factory.class.php用get_instance($db_config = '')方法返回当前工厂类,用get_database($db_name)方法返回数据库操作实例(它调用工厂类的connect($db_name)来实现工厂类原理),
/**
* 加载数据库驱动
* @param $db_name 数据库配置名称
* @return object
*/
public function connect($db_name) {
$object = null;
switch($this->db_config[$db_name]['type']) {
case 'mysql' :
pc_base::load_sys_class('mysql', '', 0);
$object = new mysql();
break;
case 'mysqli' :
$object = pc_base::load_sys_class('mysqli');
break;
case 'access' :
$object = pc_base::load_sys_class('db_access');
break;
default :
pc_base::load_sys_class('mysql', '', 0);
$object = new mysql();
}
$object->open($this->db_config[$db_name]);
return $object;
}
(3)model.class.php加载工厂类(pc_base::load_sys_class('db_factory', '', 0);),提供数据库的curd操作方法。
$this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);//获取数据库实例
总结:通过工厂类的get_instance()获取当前实例,调用工厂方法的get_database()连接数据库并返回数据库操作实例,并返回给model的$this->db,之后所有方法的调用直接通过$this->db调用model里的方法即可。
本文标签:
很赞哦! ()
相关教程
图文教程
PHPCMSv9安全设置教程
一、目录权限设置很重要:可以有效防范黑客上传木马文件.如果通过 chmod 644 * -R 的话,php文件就没有权限访问了。如果通过chmod 755 * -R 的话,php文件的权限就高了。
PHPCMS搭建wap手机网站的方法
PHPCMS搭建PC端网站比较方便,但是在wap手机端方面却不怎么实用,而且自带的手机建站感觉不是很好,而且模版不好控制,现在对其进行修改,手机建站个人感觉比较方便
phpcms修改代码去除网站后台公告的方法
首先打开phpcms/modules/admin/functions/admin.func.php文件 (建议使用高级编辑器打开)然后找到下面两行代码
phpcmsv9分页代码示例
phpcms的分页很简单,只需在需要分页的地方写入如下代码即可:连样式都有了,如果你是调用的cms本身的css的话。我们可以自己给$pages传值的,
相关源码
-
html5响应式pbootcms模板新闻资讯博客网站源码该模板采用PbootCMS内核开发,专为新闻资讯类网站打造,同时具备高度行业适配性--只需替换图文内容即可快速转型为企业官网、行业门户等各类站点。查看源码 -
自适应手机版五金机械阀门设备通用行业网站模板该PbootCMS内核开发的网站模板适用于阀门设备、五金机械类企业,通过更换文字图片也可快速适配其他工业领域。采用响应式设计,确保PC端与手机端数据同步,操作便捷,并附带测试数据。查看源码 -
(自适应HTML5)响应式智能设备人工智能机器pbootcms源码免费下载这款基于PbootCMS开发的网站模板为人工智能和智能设备行业设计,采用现代化科技风格,突出产品的智能特性和技术创新。模板架构合理,功能完善,能够有效展示各类智能产品的核心功能和解决方案。查看源码 -
响应式HTML5家居建材办公家具桌椅pbootcms模板为家居建材、办公家具企业打造的响应式网站模板,同时支持多行业快速适配通过替换文字图片即可转型为其他行业官网,大幅降低开发成本。查看源码 -
(自适应)WordPress二次元博客主题SakurairoSakurairo主题为二次元内容创作者设计,提供丰富的动漫风格元素和个性化的展示效果。该主题在原有Sakura主题基础上进行了功能增强,支持多种自定义设置,满足动漫爱好者建立个人博客的需求。查看源码 -
pbootcms模板(PC+WAP)微信小程序开发公司网站本模板为微信小程序开发代理、软件开发公司等企业设计,基于PbootCMS内核开发,支持PC+WAP双端响应式布局,数据实时同步,适用于多行业快速建站。查看源码
| 分享笔记 (共有 篇笔记) |
