TP框架,根据当前应用状态对应的配置文件

  index.php

define('APP_STATUS','website');

/ThinkPHP/Library/Think/Dispatcher.class.php

    /**
     * 应用程序初始化
     * @access public
     * @return void
     */
    static public function start() {
  ......
// 读取当前应用状态对应的配置文件
      if(APP_STATUS && is_file(CONF_PATH.APP_STATUS.CONF_EXT)){
        echo CONF_PATH.APP_STATUS.CONF_EXT;exit();
          C(include CONF_PATH.APP_STATUS.CONF_EXT);   //包含文件为"./Apps/Common/Conf/website.php",先看website.php

      }  
  }

项目目录/Apps/Common/Conf/website.php

<?php return M("Config")->getField("config_name,config_value"); 

website.php用M方法,获取表config的配置记录,当然也可以不连接数据库,直接返回数组

/ThinkPHP/Common/functions.php

function C($name=null, $value=null,$default=null) {
    static $_config = array();
    // 无参数时获取所有
    if (empty($name)) {
        return $_config;
    }
    // 优先执行设置获取或赋值
    if (is_string($name)) {
        if (!strpos($name, '.')) {
            $name = strtoupper($name);
            if (is_null($value))
                return isset($_config[$name]) ? $_config[$name] : $default;
            $_config[$name] = $value;
            return null;
        }
        // 二维数组设置和获取支持
        $name = explode('.', $name);
        $name[0]   =  strtoupper($name[0]);
        if (is_null($value))
            return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : $default;
        $_config[$name[0]][$name[1]] = $value;
        return null;
    }
    // 批量设置
    if (is_array($name)){//走这一步,因为实参是数组
        $_config = array_merge($_config, array_change_key_case($name,CASE_UPPER));
        return null;
    }
    return null; // 避免非法参数
}

所以,index.php中的APP_PATH决定了加载哪一个配置文件.

 

posted @ 2016-11-21 13:50  toDoYourBest  阅读(234)  评论(0编辑  收藏  举报