💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] * * * * * ## 1 配置文件——静态结构 ### 1 全局变量 ~~~ 自定义 /public/index.php 框架默认 /thinkphp/base.php 模块变量 /thinkphp/library/think/App.php ~~~ ### 2 模式配置 ~~~ 框架默认 /thinkphp/mode/common.php sae模式 /thinkphp/mode/sae.php 控制台模式 /thinkphp/mode/console.php ~~~ ### 3 全局配置 ~~~ 框架默认 /thinkphp/convention.php ~~~ ### 4 应用配置 ~~~ 应用数据库配置 /application/database.php 应用路由配置 /application/route.php ~~~ ### 5 模块配置 ~~~ 应用默认配置 /application/config.php ~~~ ## 2 配置操作——动态组织 ### 1 配置加载 >[info] 全局变量 ~~~ /public/index.php: define('APP_PATH', __DIR__ . '/../test/'); define('APP_DEBUG', false); defined('APP_AUTO_BUILD',true); ~~~ ~~~ /thinkphp/start.php: require __DIR__ . '/base.php'; ~~~ ~~~ /thinkphp/library/think/App::module(): define('MODULE_NAME', strip_tags($module)); define('MODULE_PATH', APP_PATH . MODULE_NAME . DS); define('VIEW_PATH', MODULE_PATH . VIEW_LAYER . DS); define('CONTROLLER_NAME', Config::get('url_controller_convert') ? strtolower($controllerName) : $controllerName); define('ACTION_NAME', Config::get('url_action_convert') ? strtolower($actionName) : $actionName); ~~~ ~~~ /thinkphp/library/think/App::route(): define('__INFO__', $_SERVER['PATH_INFO']); define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION))); ~~~ >[info] 模式配置加载 ~~~ /thinkphp/start.php: $mode = require MODE_PATH . APP_MODE . EXT; ~~~ >[info] 全局配置加载 ~~~ /thinkphp/mode/common.php: 'config' => THINK_PATH . 'convention' . EXT, ~~~ >[info] 应用配置加载 ~~~ /thinkphp/convention.php: 'extra_config_list' => ['database', 'route', 'validate', 'auto'] ~~~ ~~~ /thinkphp/library/think/App.php::run(): 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] 模块配置加载 ~~~ /thinkphp/library/think/App.php::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)); } } ~~~ ### 2 配置操作 >[info] 配置文件加载 `public static function load($file, $name = '', $range = '')` > $file 待解析配置文件 > $name ?? > $range 配置键名作用域 * * * * * >[info] 配置内容解析 `public static function parse($config, $type = '', $range = '')` > $config 待解析配置内容 > $type 配置解析器 > $range 配置键名作用域 * * * * * >[info] 配置参数读写 ~~~ public static function get($name = null, $range = '') public static function set($name, $value = null, $range = '') public static function has($name, $range = '') public static function reset($range = '') ~~~ > $name:配置键名 > $value:配置键值 > $range:配置键名作用域 * * * * * >[info] 框架主流程配置操作实例 ~~~ thinkphp/start.php: if (isset($mode['config'])) { is_array($mode['config']) ? Config::set($mode['config']) : Config::load($mode['config']); } ~~~ ~~~ thinkphp/library/think/App.php::run(): self::initModule(COMMON_MODULE, Config::get()); $config = Config::get(); ~~~ ~~~ thinkphp/library/think/App.php::intModule(): $config = Config::load(APP_PATH . $module . 'config' . EXT); $config = Config::load(APP_PATH . $module . $config['app_status'] . EXT); Config::load($file, is_string($name) ? $name : pathinfo($file, PATHINFO_FILENAME)); ~~~ >[info] 框架工具文件配置操作实例 ### 3 配置解析 >[info] 解析入口 ~~~ thinkphp/library/think/Config::parse(): public static function parse($config, $type = '', $range = '') { $range = $range ?: self::$range; 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); } ~~~ >[info] 解析器实现 `thinkphp/library/think/config/driver/Ini.php Ini配置内容解析` `thinkphp/library/think/config/driver/Xml.php Xml配置内容解析`