ThinkPHP的配置非常灵活,可自定义加载.大概看了一下,一共有这几个地方会加载配置文件,方便以后的读取

/***获取和设置配置参数支持批量定义**@paramstring|array$name*配置变量*@parammixed$value*配置值*@returnmixed*/functionC($name=null,$value=null){static$_config=array();//无参数时获取所有if(empty($name)){if(!empty($value)&&$array=S('c_'.$value)){$_config=array_merge($_config,array_change_key_case($array));}return$_config;}//优先执行设置获取或赋值if(is_string($name)){if(!strpos($name,'.')){$name=strtolower($name);if(is_null($value))returnisset($_config[$name])?$_config[$name]:null;$_config[$name]=$value;return;}//二维数组设置和获取支持$name=explode('.',$name);$name[0]=strtolower($name[0]);if(is_null($value))returnisset($_config[$name[0]][$name[1]])?$_config[$name[0]][$name[1]]:null;$_config[$name[0]][$name[1]]=$value;return;}//批量设置if(is_array($name)){$_config=array_merge($_config,array_change_key_case($name));if(!empty($value)){//保存配置值S('c_'.$value,$_config);}return;}returnnull;//避免非法参数}


C()函数在运行的时候,就会把配置文件中的配置都加载到C()函数中,以后只要需要的提取出来即可,而且可以临时增加自己的C函数

1.Think.class.php buildApp方法,加载公共配置文件Conf/convention.php,缓存到C方法里

//加载核心惯例配置文件Think.class.php第60行C(includeTHINK_PATH.'Conf/convention.php');if(isset($mode['config'])){//加载模式配置文件C(is_array($mode['config'])?$mode['config']:include$mode['config']);}


2.加载项目的config.php文件

//加载项目配置文件Think.class.php第66行if(is_file(CONF_PATH.'config.php'))C(includeCONF_PATH.'config.php');


3.加载系统标签配置文件ThinkPHP/Conf/tags.php文件,C('extends');

//加载模式系统行为定义if(C('APP_TAGS_ON')){if(isset($mode['extends'])){C('extends',is_array($mode['extends'])?$mode['extends']:include$mode['extends']);}else{//默认加载系统行为扩展定义C('extends',includeTHINK_PATH.'Conf/tags.php');}}


4.加载应用标签配置APP/Conf/tags.php C('extends');

//加载应用行为定义if(isset($mode['tags'])){C('tags',is_array($mode['tags'])?$mode['tags']:include$mode['tags']);}elseif(is_file(CONF_PATH.'tags.php')){//默认加载项目配置目录的tags文件定义C('tags',includeCONF_PATH.'tags.php');}

5.如果是调试模式,则加载ThinkPHP/Conf/debug.php,和应用状态调试文件

if(APP_DEBUG){//调试模式加载系统默认的配置文件C(includeTHINK_PATH.'Conf/debug.php');//读取调试模式的应用状态$status=C('APP_STATUS');//加载对应的项目配置文件if(is_file(CONF_PATH.$status.'.php'))//允许项目增加开发模式配置定义C(includeCONF_PATH.$status.'.php');}else{//部署模式下面生成编译文件build_runtime_cache($compile);}


6.App:init 调用function.php中得load_ext_file函数,加载自定义配置文件

在function.php中load_ext_file()函数中

/***加载动态扩展文件*@returnvoid*/functionload_ext_file(){//加载自定义外部文件if(C('LOAD_EXT_FILE')){$files=explode(',',C('LOAD_EXT_FILE'));foreach($filesas$file){$file=COMMON_PATH.$file.'.php';if(is_file($file))include$file;}}//加载自定义的动态配置文件if(C('LOAD_EXT_CONFIG')){$configs=C('LOAD_EXT_CONFIG');if(is_string($configs))$configs=explode(',',$configs);foreach($configsas$key=>$config){$file=CONF_PATH.$config.'.php';if(is_file($file)){is_numeric($key)?C(include$file):C($key,include$file);}}}}