Detailed explanation of configuration reading and C methods in thinkPHP
Dec 30, 2016 am 09:35 AMThe 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
<?php /** *項(xiàng)目公共配置 *@package *@author **/ return array( 'LOAD_EXT_CONFIG' => '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前綴 避免沖突 /*定義模版標(biāo)簽*/ 'TMPL_L_DELIM' =>'{sh:', //模板引擎普通標(biāo)簽開始標(biāo)記 'TMPL_R_DELIM' =>'}', //模板引擎普通標(biāo)簽結(jié)束標(biāo)記 'TMPL_CACHE_ON' => false, //關(guān)閉模板緩存 'DEFAULT_GROUP' => 'Home', //默認(rèn)訪問分組,設(shè)置默認(rèn)入口 'APP_GROUP_LIST' => 'Agent,Home,System,User,Store,Wap,Mall,Opener', // 項(xiàng)目分組設(shè)定,多個(gè)組之間用逗號(hào)分隔,例如'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', //項(xiàng)目分組設(shè)定 'DEFAULT_GROUP' => 'Home', //默認(rèn)分組
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('參數(shù)名稱')//獲取已經(jīng)設(shè)置的參數(shù)值
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')//獲取用戶配置中的用戶類型設(shè)置
##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, //靜態(tài)緩存有效期(秒) '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
/** * 獲取和設(shè)置配置參數(shù) 支持批量定義 * @param string|array $name 配置變量 * @param mixed $value 配置值 * @return mixed */ function C($name=null, $value=null) { static $_config = array(); // 無參數(shù)時(shí)獲取所有 if (empty($name)) { if(!empty($value) && $array = cache('c_'.$value)) { $_config = array_merge($_config, array_change_key_case($array)); } return $_config; } // 優(yōu)先執(zhí)行設(shè)置獲取或賦值 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; } // 二維數(shù)組設(shè)置和獲取支持 $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; } // 批量設(shè)置 if (is_array($name)){ $_config = array_merge($_config, array_change_key_case($name)); if(!empty($value)) {// 保存配置值 cache('c_'.$value,$_config); } return; } return null; // 避免非法參數(shù) }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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)