多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] * * * * * ## 1 文件 tp的配置解析驱动目录 thinkphp\library\think\config\driver\ 通过实现parse()方法对相应格式文件进行解析 包含以下2种 * * * * * 1 Ini.php Ini配置格式文件 2 Xml.php Xml配置格式文件 * * * * * ## 2 配置文件的类型与加载顺序 > 1 全局默认配置文件 think5框架内置全局配置文件 thinkphp\convention.php 包含了默认配置参数。 全局配置文件在[模式配置](http://www.kancloud.cn/zmwtp/tp5/119432)文件 mode\common.php中,使用如下语句引入 ~~~ 'config' => THINK_PATH . 'convention' . EXT, ~~~ 全局默认配置文件是配置文件的总入口。有关说明见 [附:全局配置](http://www.kancloud.cn/zmwtp/tp5/119434) > 2 应用配置 在[全局配置文件](http://www.kancloud.cn/zmwtp/tp5/119434),可以使用如下语句引入应用扩展配置文件 ~~~ 'extra_config_list' => ['database', 'route', 'validate', 'auto'], ~~~ application\目录下的database.php,route.php对应这样的扩展配置文件 > 3 模块配置 此外在App.php中的initModule()方法初始化模块中加载了模块配置文件。 单模块的模块配置文件就是appilication\config.php文件 多模块的模块配置文件在对应模块目录applicaiton\module\config.php文件。 每个模块还可以自定义扩展配置文件,使用符号"."可以定义扩展配置文件的目录 > 4 配置文件加载顺序 * * * * * >[info] 1 全局配置文件加载 在[框架引导文件](http://www.kancloud.cn/zmwtp/tp5/119425)中加载[模式配置文](http://www.kancloud.cn/zmwtp/tp5/119432)件中定义的[全局配置文件](http://www.kancloud.cn/zmwtp/tp5/119434),代码如下。 ~~~ if (isset($mode['config'])) { is_array($mode['config']) ? Config::set($mode['config']) : Config::load($mode['config']); } ~~~ >[info] 2 应用扩展配置文件加载 在[应用启动](http://www.kancloud.cn/zmwtp/tp5/119426)中,加载[全局配置文件](http://www.kancloud.cn/zmwtp/tp5/119434)中定义的扩展配置文件,代码如下。 ~~~ if (!empty($config['extra_file_list'])) { foreach ($config['extra_file_list'] as $file) { $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; if (is_file($file)) { include_once $file; } } } ~~~ >[info] 3 模块配置文件加载 在[应用启动](http://www.kancloud.cn/zmwtp/tp5/119429)的initModule()中,加载模块定义的配置文件,代码如下 ~~~ $config = Config::load(APP_PATH . $module . 'config' . EXT); if ($config['app_status']) { $config = Config::load(APP_PATH . $module . $config['app_status'] . EXT); } if ($config['extra_config_list']) { foreach ($config['extra_config_list'] as $name => $file) { $file = strpos($file, '.') ? $file : $path . $file . EXT; Config::load($file, is_string($name) ? $name : pathinfo($file, PATHINFO_FILENAME)); } } ~~~ 由上分析可知,配置文件的加载顺序是[模式配置文件](http://www.kancloud.cn/zmwtp/tp5/119432)->[全局配置文件](http://www.kancloud.cn/zmwtp/tp5/119434)->扩展配置文件->模块配置文件。 ## 3 配置文件的解析文件 所有配置文件使用[配置解析器](http://www.kancloud.cn/zmwtp/tp5/119434)thinkphp\library\think\Config.php, 由前面分析可知主要定义了以下三类接口: >[info] set() get() reset() has()配置内容操作 > load() parse()配置文件解析与加载 > range()配置作用域设置 所有的配置项最后保存到Config.php的静态成员变量$config=[]数组中。 其中的parse()中根据参数$type调用不同的配置解析驱动,代码如下 ~~~ if (empty($type)) { $type = pathinfo($config, PATHINFO_EXTENSION); } $class = (false === strpos($type, '\\')) ? '\\think\\config\\driver\\' . ucwords($type) : $type; self::set((new $class())->parse($config), '', $range); ~~~ 根据$type参数,创建不同的解析驱动类new $class()解析配置内容$config, 这里的$class对应config\driver\目录的解析类Ini,Xml解析驱动。 ## 4 总结 >[info] 1 配置文件 全局配置文件convention.php 应用扩展配置database.php route.php 模块配置文件config.php >[info] 2 配置解析类 thinkphp\library\think\Config.php >[info] 3 配置操作 load() parse() set() get() reset() has()