您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 辽源分类信息网,免费分类信息发布

ThinkPHP应用模式扩展详解_PHP

2024/7/24 3:42:42发布32次查看
thinkphp的应用模式使得开发人员对核心框架进行改造较以往更为得心应手,并且可以让你的应用适应更多的环境和不同的需求。每个应用模式都有自己的模式定义文件,相对于thinkphp3.1版本,thinkphp3.2版本对应用模式的扩展更加明确和清晰,在thinkphp3.1版本中定义了cli、lite、thin、amf、phprpc、rest模式,其定义方式和thinkphp3.2版本的方式大同小异,如有需要可以参考修改,其中cli模式被thinkphp框架内置,不用单独定义cli模式即可正常使用,如需要更细化调整可以参考3.1版编写的cli运行模式扩展。thinkphp同样提供了便捷的开发环境和正式环境的模式切换方式。让我们随着thinkphp的运行流程解析其应用模式扩展之谜。
一、应用模式的使用
在研究应用模式扩展之前,看看如何使用应用模式的吧。一般通过在入口文件定义常量app_mode为应用模式名称,但是在分析thinkphp框架入口文件时,了解到框架默认采用模式为普通模式(common),而且可以自动识别sae环境,当然前提是没有定义app_mode常量时,当然thinkphp可以自动识别cli和cgi模式,并且在cli和cgi环境下运行thinkphp框架在默认模式中自动对这两种环境做了细微调整,当然也可以自己扩展这两种应用模式。
if(function_exists('saeautoloader')){// 自动识别sae环境 defined('app_mode') or define('app_mode', 'sae'); defined('storage_type') or define('storage_type', 'sae');}else{ defined('app_mode') or define('app_mode', 'common'); // 应用模式 默认为普通模式 defined('storage_type') or define('storage_type', 'file'); // 存储类型 默认为file }
二、应用模式定义
在thinkphp框架当中除了thinkphp框架入口和框架引导类以外,基本所有其他功能都可以通过应用模式进行更改和扩展,如果我们要增加一个应用模式,只需要在thinkphp\mode目录下面定义一个模式定义文件即可,我们可以通过分析common模式进行学习。该文件的代码如下:
//文件路径:thinkphp/mode/common.php/** * thinkphp 普通模式定义 * 定义一个模式文件,只需要返回一个模式包含文件的数组即可 * 在数组中主要包含4种扩展文件列表: * config 为默认加载配置文件列表 * alias 为核心类库别名配置列表 * core 需要加载的核心函数和类文件列表 * tags 行为配置列表 * * 如果在应用模式定义中加载一个自定类,那个自定义类的命名空间必须是think */return array( // 配置文件 'config' => array( think_path.'conf/convention.php', // 系统惯例配置 conf_path.'config.php', // 应用公共配置 ), // 别名定义 'alias' => array( 'think\log' => core_path . 'log'.ext, 'think\log\driver\file' => core_path . 'log/driver/file'.ext, 'think\exception' => core_path . 'exception'.ext, 'think\model' => core_path . 'model'.ext, 'think\db' => core_path . 'db'.ext, 'think\template' => core_path . 'template'.ext, 'think\cache' => core_path . 'cache'.ext, 'think\cache\driver\file' => core_path . 'cache/driver/file'.ext, 'think\storage' => core_path . 'storage'.ext, ), // 函数和类文件 'core' => array( think_path.'common/functions.php', common_path.'common/function.php', core_path . 'hook'.ext, core_path . 'app'.ext, core_path . 'dispatcher'.ext, //core_path . 'log'.ext, core_path . 'route'.ext, core_path . 'controller'.ext, core_path . 'view'.ext, behavior_path . 'buildlitebehavior'.ext, behavior_path . 'parsetemplatebehavior'.ext, behavior_path . 'contentreplacebehavior'.ext, ), // 行为扩展定义 'tags' => array( 'app_init' => array( 'behavior\buildlitebehavior', // 生成运行lite文件 ), 'app_begin' => array( 'behavior\readhtmlcachebehavior', // 读取静态缓存 ), 'app_end' => array( 'behavior\showpagetracebehavior', // 页面trace显示 ), 'view_parse' => array( 'behavior\parsetemplatebehavior', // 模板解析 支持php、内置模板引擎和第三方模板引擎 ), 'template_filter'=> array( 'behavior\contentreplacebehavior', // 模板输出替换 ), 'view_filter' => array( 'behavior\writehtmlcachebehavior', // 写入静态缓存 ), ),);
我们看到这个普通应用模式代码之后,有点明了thinkphp的应用模式扩展是怎么回事了,但是还是知其然而不知其所以然,定义一个加载文件列表和配置是如何改变框架核心的呢?秘密就在thinkphp引导类中,让我们再回顾以下吧!
//判断是否存在core.php配置文件(这是开发环境临时定义的运行模式,我是这么理解的) //否者加载app_mode定义的模式文件 $mode = include is_file(conf_path.'core.php')?conf_path.'core.php':mode_path.app_mode.'.php'; //加载模式中core定义的核心文件列表 foreach ($mode['core'] as $file){ if(is_file($file)) { include $file; if(!app_debug) $content .= compile($file); } } //加载模式中定义的config配置文件列表 foreach ($mode['config'] as $key=>$file){ is_numeric($key)?c(include $file):c($key,include $file); } // 读取当前应用模式对应的配置文件 if('common' != app_mode && is_file(conf_path.'config_'.app_mode.'.php')) c(include conf_path.'config_'.app_mode.'.php'); // 加载模式中alias别名列表定义 if(isset($mode['alias'])){ self::addmap(is_array($mode['alias'])?$mode['alias']:include $mode['alias']); } // 加载应用别名定义文件 if(is_file(conf_path.'alias.php')) self::addmap(include conf_path.'alias.php'); // 加载模式中tags行为定义 if(isset($mode['tags'])) { hook::import(is_array($mode['tags'])?$mode['tags']:include $mode['tags']); } // 加载应用行为定义 if(is_file(conf_path.'tags.php')) // 允许应用增加开发模式配置定义 hook::import(include conf_path.'tags.php'); // 加载框架底层语言包 l(include think_path.'lang/'.strtolower(c('default_lang')).'.php');
通过thinkphp::start()中的这段代码,完美无缝关联的模式定义文件的意义与实现方法。
三、定义简单的运行模式
手册中有一个模式扩展到的实例,可以拿到这里来分析一下,定义一个lite简洁运行模式,首先在thinkphp/mode目录下新建一个lite.php文件内容定义如下:
return array( // 配置文件 'config' => array( think_path.'conf/convention.php', // 系统惯例配置 conf_path.'config.php', // 应用公共配置 ), // 别名定义 'alias' => array( 'think\exception' => core_path . 'exception'.ext, 'think\model' => core_path . 'model'.ext, 'think\db' => core_path . 'db'.ext, 'think\cache' => core_path . 'cache'.ext, 'think\cache\driver\file' => core_path . 'cache/driver/file'.ext, 'think\storage' => core_path . 'storage'.ext, ), // 函数和类文件 'core' => array( mode_path.'lite/functions.php', common_path.'common/function.php', mode_path . 'lite/app'.ext, mode_path . 'lite/dispatcher'.ext, mode_path . 'lite/controller'.ext, mode_path . 'lite/view'.ext, core_path . 'behavior'.ext, ), // 行为扩展定义 'tags' => array( 'view_parse' => array( 'behavior\parsetemplate', // 模板解析 支持php、内置模板引擎和第三方模板引擎 ), 'template_filter'=> array( 'behavior\contentreplace', // 模板输出替换 ), ),);
从上面的配置当中我们发现core中的核心文件大多数都被替换了,当然这些需要被替换的程序功能需要我们自己去实现,不过建议大家直接拷贝普通模式中定义的核心文件过来修改。接下来我们来实现以下thinkphp应用开发中的核心类库扩展文件app.class.php
在thinkphp/mode目录下建立一个lite目录并在lite目录下建立app.class.php文件,以下是程序文件的实现:
//模式扩展类库必须是think命名空间namespace think; /** * thinkphp 应用程序类 执行应用过程管理 lite模式扩展类 * 实现thinkphp核心类库扩展时,尽可能仿造原有类库实现(除非对thinkphp框架源码特别了解) * 因为在其他没有扩展的核心文件中可能会调用扩展的核心类文件中的某个方法,除非你打算全部扩展 */class app{/** * 应用程序初始化 * @access public * @return void */static public function init() { //具体现实} /** * 执行应用程序 * @access public * @return void */static public function exec() { //具体实现} /** * 运行应用实例 入口文件使用的快捷方法 * @access public * @return void */static public function run() { //具体实现} static public function logo(){ //具体实现}}
当文件所有扩展文件的实现后,可以在框架入口文件定义app_mode常量为lite。
另外需要注意一点,手册当中要求定义的mode_name常量来改变运行模式是之前3.1版本中定义运行模式的方法,使用新版本的用户对此需要注意。
辽源分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录