Home  >  Article  >  php教程  >  Detailed explanation of configuration reading and C methods in thinkPHP

Detailed explanation of configuration reading and C methods in thinkPHP

高洛峰
高洛峰Original
2016-12-30 09:35:331215browse

The examples in this article describe the reading and C methods of configuration in thinkPHP. Share it with everyone for your reference, the details are as follows:

1. Project public configuration

Conf/config.php

The content is as follows

 'db,info,email,safe,upfile,cache,route,app,alipay,sms,platform,store,pay', 
 'APP_AUTOLOAD_PATH'  => '@.ORG',
 'OUTPUT_ENCODE'   => true,    //页面压缩输出
 'PAGE_NUM'    => 15,
 /*Cookie配置*/
 'COOKIE_PATH'   => '/',    // Cookie路径
 'COOKIE_PREFIX'   => '',    // Cookie前缀 避免冲突
 /*定义模版标签*/
 'TMPL_L_DELIM'   =>'{sh:',   //模板引擎普通标签开始标记
 'TMPL_R_DELIM'   =>'}',    //模板引擎普通标签结束标记
 'TMPL_CACHE_ON'   => false,   //关闭模板缓存
 'DEFAULT_GROUP'   => 'Home',   //默认访问分组,设置默认入口
 'APP_GROUP_LIST'  => 'Agent,Home,System,User,Store,Wap,Mall,Opener',  // 项目分组设定,多个组之间用逗号分隔,例如'Home,Admin'
 'PUBLIC_RESOURSE'  => './Public/',
 'URL_404_REDIRECT'  => './Tpl/404.html',
);
?>

'LOAD_EXT_CONFIG' => 'db,info,email,safe,upfile,cache,route,app,alipay,sms,platform,store,pay' determines the additional configuration loaded, these Configurations can be read through the C() method and are globally valid.

2. If module grouping is enabled, you can define a configuration file for each group separately. The group configuration file is located at:

Project configuration directory/group name/config.php

'APP_GROUP_LIST' => 'Home,Admin', //项目分组设定
'DEFAULT_GROUP' => 'Home', //默认分组

Now that two groups, Home and Admin, are defined, we can define the group configuration file as follows:

Conf/Home/config.php
Conf/Admin/config.php

The configuration file of each group is only valid in the current group. The definition format of the group configuration is the same as the project configuration.

Note: The group name is case-sensitive and must be consistent with the defined group name.

3. Read the configuration

After defining the configuration file, you can use the C method provided by the system (if you feel it is strange, you can use the Config word to help remember) to read the existing configuration file. Configuration

C('参数名称')//获取已经设置的参数值

For example, C('APP_STATUS') can read the setting value of the system's debug mode. Similarly, since the configuration parameters are not case-sensitive , so C('app_status') is equivalent, but uppercase convention is recommended.

If APP_STATUS does not yet exist, return NULL.

The C method can also be used to read the two-dimensional configuration

C('USER_CONFIG.USER_TYPE')//获取用户配置中的用户类型设置

##The C method reads the global configuration and the configuration of the current module.

If there is no parameter, all valid configurations will be read.

If the same configuration name exists, the previous value will be overwritten.

For example:

'HTML_CACHE_TIME' => 60, //静态缓存有效期(秒)
'HTML_CACHE_TIME' => 80,

The final result obtained is 80.

The loading sequence is based on the parameter LOAD_EXT_CONFIG

'LOAD_EXT_CONFIG' => 'db,info,email,safe,upfile,cache,route,app,alipay,sms,platform,store,pay'

For example, there is a parameter HTML_CACHE_TIME in info that is 60, but there is no parameter in other configurations, then this parameter is read It's 60 when it comes out.

If HTML_CACHE_TIME is 50 in the db, the value is still 60. Because info is read later, the HTML_CACHE_TIME in the db is overwritten.

Attached is the C method source code

/**
 * 获取和设置配置参数 支持批量定义
 * @param string|array $name 配置变量
 * @param mixed $value 配置值
 * @return mixed
 */
function C($name=null, $value=null) {
 static $_config = array();
 // 无参数时获取所有
 if (empty($name)) {
  if(!empty($value) && $array = cache('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))
    return isset($_config[$name]) ? $_config[$name] : null;
   $_config[$name] = $value;
   return;
  }
  // 二维数组设置和获取支持
  $name = explode('.', $name);
  $name[0] = strtolower($name[0]);
  if (is_null($value))
   return isset($_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)) {// 保存配置值
   cache('c_'.$value,$_config);
  }
  return;
 }
 return null; // 避免非法参数
}

I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.

For more detailed explanations of reading and C methods configured in thinkPHP, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn