多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] * * * * * ## 1 模式配置文件源代码(以/thinkphp/mode/common.php为例,console.php,sae.php模式同理) ~~~ return [ // 命名空间 'namespace' => [ 'think' => LIB_PATH . 'think' . DS, 'behavior' => LIB_PATH . 'behavior' . DS, 'traits' => LIB_PATH . 'traits' . DS, APP_NAMESPACE => APP_PATH, ], // 配置文件 'config' => THINK_PATH . 'convention' . EXT, // 别名定义 'alias' => [ 'think\App' => CORE_PATH . 'App' . EXT, 'think\Build' => CORE_PATH . 'Build' . EXT, 'think\Cache' => CORE_PATH . 'Cache' . EXT, 'think\Config' => CORE_PATH . 'Config' . EXT, 'think\Controller' => CORE_PATH . 'Controller' . EXT, 'think\Cookie' => CORE_PATH . 'Cookie' . EXT, 'think\Db' => CORE_PATH . 'Db' . EXT, 'think\Debug' => CORE_PATH . 'Debug' . EXT, 'think\Error' => CORE_PATH . 'Error' . EXT, 'think\Exception' => CORE_PATH . 'Exception' . EXT, 'think\Hook' => CORE_PATH . 'Hook' . EXT, 'think\Input' => CORE_PATH . 'Input' . EXT, 'think\Lang' => CORE_PATH . 'Lang' . EXT, 'think\Log' => CORE_PATH . 'Log' . EXT, 'think\Model' => CORE_PATH . 'Model' . EXT, 'think\Response' => CORE_PATH . 'Response' . EXT, 'think\Route' => CORE_PATH . 'Route' . EXT, 'think\Session' => CORE_PATH . 'Session' . EXT, 'think\Template' => CORE_PATH . 'Template' . EXT, 'think\Url' => CORE_PATH . 'Url' . EXT, 'think\View' => CORE_PATH . 'View' . EXT, 'think\db\Driver' => CORE_PATH . 'db' . DS . 'Driver' . EXT, 'think\view\driver\Think' => CORE_PATH . 'view' . DS . 'driver' . DS . 'Think' . EXT, 'think\template\driver\File' => CORE_PATH . 'template' . DS . 'driver' . DS . 'File' . EXT, 'think\log\driver\File' => CORE_PATH . 'log' . DS . 'driver' . DS . 'File' . EXT, 'think\cache\driver\File' => CORE_PATH . 'cache' . DS . 'driver' . DS . 'File' . EXT, ], ]; ~~~ ## 2 分析 模式配置文件用来配置框架的运行环境。 common.php 通用运行环境, sae.php 新浪云运行环境, console.php 命令行运行环境 `defined('APP_MODE') or define('APP_MODE', function_exists('saeAutoLoader') ? 'sae' : 'common');` 运行模式由全局变量文件/thinkphp/base.php中的APP_MODE定义 `$mode = require MODE_PATH . APP_MODE . EXT;` 在thinkphp/start.php中加载对应模式文件,并初始化运行环境。 ~~~ if (isset($mode['namespace'])) { Loader::addNamespace($mode['namespace']); } ~~~ namespace 为模式对应环境的根命名空间数组。 ~~~ if (isset($mode['alias'])) { Loader::addMap(is_array($mode['alias']) ? $mode['alias'] : include $mode['alias']); } ~~~ alias 为模式对应环境的类别名数组。 ~~~ if (isset($mode['config'])) { is_array($mode['config']) ? Config::set($mode['config']) : Config::load($mode['config']); } ~~~ config 为模式对应环境的配置数组 common模式加载thinkphp/convention.php作为默认全局配置 ~~~ if (APP_HOOK && isset($mode['tags'])) { Hook::import(is_array($mode['tags']) ? $mode['tags'] : include $mode['tags']); } ~~~ tags为模式对应环境的监听回调数组 ## 3 总结 模式配置文件通常用来配置框架运行环境信息,分为四部分。 namespace 根命名空间数组 alias 类的别名数组 config 框架的全局配置文件 tags 运行的监听回调数组 模式配置文件的使用 见 使用范例的运行模式开发