Thinkphp3.2 --- configuration module, thinkphp3.2 Module

Source: Internet
Author: User

Thinkphp3.2 --- configuration module, thinkphp3.2 Module

I. Configuration format

All configurations in the Thinkphp framework are defined in the format of Arrays:

// Return array ('default _ module' => 'index', // default module 'url _ model' => '2 ', // URL mode: 'session _ AUTH_START '=> true, // whether to enable the SESSION)

Configuration parameters are case-insensitive (both case and case are converted to lower case), but we recommend that you keep the configuration format in upper case.

You can also use the two-dimensional array mode in the configuration file to configure:

// Return array ('default _ module' => 'index', // default module 'url _ model' => '2 ', // URL mode: 'session _ AUTO_START '=> true, // whether to enable SESSION 'user _ config' => array ('user _ auth' => true, 'User _ type' => 2,), // more configuration parameters //...);

Note: The second-level configuration is case sensitive and must be consistent during reading.

 Other formats

3.2.2 versions start with configuration files addedyaml/json/xml/iniAnd supports custom formats.

We canApplication entry fileTo define the suffix of the application configuration file, for example:

define('CONF_EXT','.ini');

After the configuration is defined, all application configuration files (including module configuration files) are suffixed with. ini. This configuration does not affect the loading of the internal configuration files of the framework, but takes effect only for the custom configuration files.

The ini configuration format is as follows:

DEFAULT_MODULE = Index; default module URL_MODEL = 2; URL mode SESSION_AUTO_START = on; Whether to enable session

The XML format is configured as follows: 

<config><default_module>Index</default_module><url_model>2</url_model><session_auto_start>1</session_auto_start></config>

The yaml format is configured as follows:  

Default_module: Index # default module url_model: 2 # URL mode session_auto_start: True # enable session

The JSON format is configured as follows: 

{"default_module":"Index","url_model":2,"session_auto_start":True}

Besidesyaml/json/xml/iniYou can also customize the configuration format as follows:

Define ('conf _ EXT ','. test '); // configure the custom configuration format (suffix) define ('conf _ parse', 'parse _ test'); // the corresponding parsing function

The returned value of the parsing function defined by CONF_PARSE is a configuration array.

 

Ii. Configuration Loading

In Thinkphp, application configuration files are automatically loaded in the following order:

Conventional configuration-> application configuration-> mode configuration-> debugging configuration-> Status Configuration-> module configuration-> extended configuration-> dynamic configuration

The above is the Loading Order of the configuration file, because the subsequent configuration will overwrite the previous configuration with the same name (if it does not take effect), so the configuration priority is from right to left.

The following describes the differences and locations of different configuration files.

1. Conventional Configuration

Convention over configuration is an important concept that the system follows. The framework has a convention configuration file (located in Thinkphp/Conf/convention. php). Most of the configuration items are different or newly added according to the configuration and Convention. If the default configuration is used completely, no configuration files need to be defined.

<? Php // + keys // | ThinkPHP [we can do it just think it] // + keys // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved. // + ------------------------------------------------------------------------ // | Licensed (http://www.apache.org/licenses/LICENSE-2.0) // + Author // | Author: liu21st <liu21st@gmail.com> // + Author/*** ThinkPHP Convention profile * do not modify this file, if you want to override the value of the Convention configuration, you can set configuration items that do not match the conventions in the application configuration file * the configuration name is case-insensitive, the system will convert them to lowercase letters * all configuration parameters can be dynamically changed */defined ('Think _ path') or exit () before they take effect (); return array (/* application setting */'app _ USE_NAMESPACE '=> true, // whether the application class library uses the namespace' APP _ SUB_DOMAIN_DEPLOY '=> false, // whether to enable sub-domain name deployment 'app _ SUB_DOMAIN_RULES' => array (), // sub-domain name deployment rule 'app _ DOMAIN_SUFFIX '=> '', // If the domain name SUFFIX is a SUFFIX such as com.cn net.cn, you must set 'Action _ SUFFIX' => '', // operation method suffix 'multi _ module' => true, // whether MULTI-MODULE is allowed. If it is false, DEFAULT_MODULE 'module _ DENY_LIST '=> array ('common ', 'runtime'), 'controller _ level' => 1, 'app _ AUTOLOAD_LAYER '=> 'controller, model', // automatically loaded application class library layer closes APP_USE_NAMESPACE Then the valid value is 'app _ AUTOLOAD_PATH '=> '', // The automatically loaded path is effective after the APP_USE_NAMESPACE is disabled/* Cookie setting */'cookie _ EXPIRE' => 0, // Cookie validity period: 'cookie _ DOMAIN '=> '', // COOKIE valid DOMAIN name 'cookie _ path' => '/', // Cookie Path 'cookie _ prefix' => '', // avoid conflict with cookie prefix 'cookie _ SECURE '=> false, // Cookie secure transmission 'cookie _ HTTPONLY '=> '', // COOKIE httponly setting/* DEFAULT setting */'default _ m_lay' => 'model ', // DEFAULT model layer name 'default _ C_LAYER '=> 'controller ', // DEFAULT controller layer name 'default _ v_lay' => 'view', // DEFAULT View layer name 'default _ LANG '=> 'zh-cn ', // DEFAULT language 'default _ THEME '=> '', // DEFAULT template topic name 'default _ module' => 'home ', // DEFAULT module 'default _ controller' => 'index', // default controller name 'default _ action' => 'index ', // DEFAULT operation name 'default _ charset' => 'utf-8', // DEFAULT output encoding 'default _ TIMEZONE '=> 'prc ', // DEFAULT Time Zone 'default _ AJAX_RETURN '=> 'json', // default ajax data return format, optional json xml... 'default _ JSONP_HA NDLER '=> 'jsonprecall', // The processing method returned by the default jsonp format: 'default _ filter' => 'htmlspecialchars'. // The DEFAULT parameter filtering method is used for the I function... /* database settings */'db _ type' => '', // Database TYPE 'db _ host' => '', // server address 'db _ name' => '', // database NAME 'db _ user' => '', // username 'db _ pwd' => '', // password 'db _ port' =>'', // PORT 'db _ prefix' => '', // database table prefix 'db _ PARAMS '=> array (), // database connection parameter 'db _ debug' => TRUE, // After enabling the database debugging mode, you can record the SQL log 'db _ FIELDS_CACHE '=> true ,/ /Enable the field cache 'db _ charset' => 'utf8'. // The default utf8 'db _ DEPLOY_TYPE '=> 0 is used for database encoding. // The database deployment method is as follows: 0 centralized (Single Server), 1 distributed (Master/Slave server) 'db _ RW_SEPARATE '=> false, // whether the database read/write is separated from the Master/Slave Type valid 'db _ MASTER_NUM' => 1, // Number of primary servers after read/write splitting 'db _ slave_no' => '', // specify the serial number of the slave server/* DATA cache settings */'data _ CACHE_TIME '=> 0, // DATA cache validity period 0 indicates permanent cache 'data _ CACHE_COMPRESS '=> false, // whether the DATA cache compresses the cache 'data _ CACHE_CHECK' => false, // whether the DATA cache verifies the cache 'data _ cache_prefix' => '',/ /Cache prefix 'data _ CACHE_TYPE '=> 'file', // DATA cache type, supported: file | Db | Apc | Memcache | Shmop | Sqlite | Xcache | Apachenote | Eaccelerator 'data _ CACHE_PATH '=> TEMP_PATH, // cache path setting (only effective for File Cache) 'Data _ CACHE_KEY '=> '', // cache File KEY (effective only for File caching) 'Data _ CACHE_SUBDIR' => false, // use sub-directory cache (automatically create sub-directories Based on the hash of the cache ID) 'Data _ PATH_LEVEL '=> 1, // subdirectory cache level/* ERROR setting */'error _ message' => 'page ERROR! Please try again later ~ ', // ERROR display information, which is valid in non-debug mode: 'error _ page' => '', // The ERROR orientation PAGE 'show _ ERROR_MSG' => false, // display the error message 'trace _ MAX_RECORD '=> 100, // maximum number of error records for each level/* LOG setting */'Log _ RECORD' => false, // The default LOG 'Log _ type' => 'file' is not recorded. // The default log type is 'Log _ level' => 'emerg, ALERT, CRIT, err', // The LOG level that can be recorded 'Log _ FILE_SIZE '=> 2097152, // the LOG file size limit is 'Log _ EXCEPTION_RECORD' => false, // whether to record the exception information log/* SESSION settings */'session _ AUTO_START '=> true ,// Whether to automatically enable Session 'session _ options' => array (). // The SESSION configuration array supports the 'session _ type' => ''parameter, such as the type name id path expire domain parameter '', // The session hander type does not need to be set by default unless the 'session _ prefix' => ''driver is extended '', // session prefix // 'var _ SESSION_ID '=> 'session _ id ', // submit variable of sessionID/* template engine settings */'tmpl _ CONTENT_TYPE '=> 'text/html ', // default template output type 'tmpl _ ACTION_ERROR '=> THINK_PATH. 'tpl/dispatch_jump.tpl ', // The template file corresponding to the default error jump't MPL_ACTION_SUCCESS '=> THINK_PATH. 'tpl/dispatch_jump.tpl ', // The template file 'tmpl _ EXCEPTION_FILE' => THINK_PATH is successfully redirected by default. 'tpl/think_exception.tpl ', // template file 'tmpl _ DETECT_THEME' => false, // automatically detects the template topic 'tmpl _ TEMPLATE_SUFFIX '=> '.html ', // default template file suffix 'tmpl _ FILE_DEPR '=> '/', // delimiter between the template file CONTROLLER_NAME and ACTION_NAME // layout settings 'tmpl _ ENGINE_TYPE '=> 'think ', // default template engine the following settings are only valid for the Think template engine 'tmpl _ CACHFILE_SUFFIX '=> '. Php ', // default template cache suffix 'tmpl _ DENY_FUNC_LIST' => 'echo, exit ', // The template engine disables the function 'tmpl _ deny_php' => false, // whether the default template engine disables the PHP native code 'tmpl _ L_DELIM '=>' {', // The General tag of the template engine starts to mark 'tmpl _ R_DELIM' => '}', // tag 'tmpl _ VAR_IDENTIFY '=> 'array' at the end of a common tag in the template engine. // template variable identification. If this parameter is set to 'obj ', it indicates the object 'tmpl _ STRIP_SPACE' => true, // whether to remove html spaces and line breaks in the template file 'tmpl _ CACHE_ON '=> true, // whether to enable template compilation cache, if it is set to false, 'tmpl _ cache_prefix' => ''will be re-compiled each time. // The template cache prefix ID can be dynamically changed to 'tmpl _ CACHE_TIME '=> 0, // The template cache validity period 0 is permanent (in numbers, unit: seconds) 'tmpl _ LAYOUT_ITEM '=>' {__ CONTENT __}', // The content replacement identifier of the LAYOUT template 'layout _ on' => false, // whether to enable the LAYOUT 'layout _ name' => 'layout ', // The default layout name is layout. // The tag library settings of the Think template engine are 'taglib _ begin' =>' <', // Tag library Label Start marking 'taglib _ end' =>', // tag library label END marking 'taglib _ load' => true, // whether to use a tag library other than the built-in tag library. The default value is 'taglib _ BUILD_IN '=> 'cx ', // The name of the built-in tag Library (you do not need to specify the name of the tag library when using tags). Separate the names with commas (,). Note that the resolution sequence is 'taglib _ PRE_LOAD '=> '', // The tag library to be loaded (the name of the tag library must be specified). Multiple tag libraries are separated by commas (,)/* URL setting */'url _ CASE_INSENSITIVE '=> true, // The default value "false" indicates that the URL is case-sensitive. true indicates that the URL is case-insensitive. 'url _ model' => 1. // URL access mode. Optional parameters: 0, 1, 2, and 3, represents the following four modes: // 0 (normal mode); 1 (PATHINFO mode); 2 (REWRITE Mode); 3 (compatible mode): the default PATHINFO mode is 'url _ PATHINFO_DEPR '=>'/', // PATHINFO mode, the delimiter between parameters: 'url _ PATHINFO_FETCH '=> 'orig _ PATH_INFO, REDIRECT_PATH_INFO, REDIRECT_URL ', // The list of SERVER substitution variables used to determine the PATH_INFO parameter 'url _ REQUEST_URI '=> 'request _ URI ', // The default system variable for obtaining the current page address is REQUEST_URI 'url _ HTML_SUFFIX '=> 'html ', // set the URL pseudo-static suffix to 'url _ DENY_SUFFIX '=> 'ico | png | gif | jpg ', // set the suffix 'url _ PARAMS_BIND '=> true for URL-prohibited access. // bind the URL variable to the Action Method parameter 'url _ PARAMS_BIND_TYPE '=> 0, // Type 0 bound to the URL variable binding by variable name binding 1 binding in variable order 'url _ params_filter' => false, // bind the URL variable to filter 'url _ PARAMS_FILTER_TYPE '=> '', // bind the URL variable to the filtering method. If it is null, call DEFAULT_FILTER 'url _ ROUTER_ON' => false, // whether to enable URL routing 'url _ ROUTE_RULES '=> array (), // default routing rules for the module 'url _ MAP_RULES' => array (), // URL ing definition rule/* system variable name setting */'var _ module' => 'M ', // The default module obtains the variable 'var _ ADDON '=> 'addon', // The default plug-in controller namespace variable 'var _ CON Troroller '=> 'C', // The default controller obtains the variable 'var _ action' => 'A ', // obtain the variable 'var _ AJAX_SUBMIT '=> 'ajax' by default. // The default ajax submission variable 'var _ jsonp_handler' => 'callback ', 'var _ pathinfo' =>'s ', // The compatible pattern PATHINFO obtains the variable such? The parameters after s =/module/action/id/1 depend on URL_PATHINFO_DEPR 'var _ template' => 'T ', // default template switch variable 'var _ AUTO_STRING '=> false, // whether the input variable is automatically forcibly converted to a string. If enabled, the array variable must be manually passed in the variable modifier to obtain the variable 'HTTP _ CACHE_CONTROL '=> 'private ', // webpage Cache Control 'check _ APP_DIR '=> true, // CHECK whether the 'file _ UPLOAD_TYPE' => 'local' is created in the application directory ', // File Upload method 'data _ CRYPT_TYPE '=> 'think', // DATA encryption method );

2. Application configuration 

The Application configuration file is the public configuration file that will be loaded before calling all modules (default: Application/Common/Conf/config. php)

Note: If the public module name is changed, the location of the Public configuration file should correspond

3. mode configuration

If you use an application mode other than the common application mode, you can also define a configuration file for the application mode. The file name specifications are as follows:Application/Common/Conf/config _ Application Mode name. php(It is loaded only when this mode is running ).

4. debug Configuration 

If the debug mode is enabled, the Framework debugging configuration file (located at ThinkPHP/Conf/debug) is automatically loaded. php) and the Application configuration debugging file (at: Application/Common/Conf/debug. php)

5. Status Configuration

Each application can set its own State (or Application Scenario) under different circumstances and load different configuration files.

For example, you need to set up different database test environments in the company and in the home. In this way, we can define in the company's entry file:

define('APP_STATUS','office');

Then the configuration file (in Application/Common/Conf/office. php) corresponding to the status change is automatically loaded)

If we get home, we define it:

define('APP_STATUS','home');

The configuration file (in Application/Common/Conf/home. php) corresponding to the status is automatically loaded)

Status Configuration is optional

6. module configuration

Each module automatically loads its own configuration file (in Application/current module/Conf/conf. php ).

If other Application modes are used, you can also define a configuration file for the Application mode. The naming rule is Application/current module/Conf/conf _ Application Mode name. php.

The module can also support independent state configuration files. The naming rules are Application/current module/Conf/Application Status. php.

Iii. Read Configuration 

Regardless of the configuration file, after defining the configuration file, you can use the C method provided by the system (you can use the Config word to help remember) to read the existing configuration.

Obtain the set parameter values:C ('parameter name ')

For example:

C('URL_MODEL')

You can read the setting values in the debugging mode of the system. Similarly, the configuration parameters are case-insensitive. Therefore, the parameter names in method C are case-insensitive, but we recommend that you use uppercase letters for the standard.

Note: The configuration parameters cannot contain "." Or special characters, and can contain letters, underscores, and numbers.

If the parameter name is a defined configuration, the return value is null.

The C method can also be used to read two-dimensional configurations.

C('USER_CONFIG.USER_TYPE');

Because the configuration parameters are globally valid, the C method can read any configuration anywhere, even if a configuration parameter has expired.

4. dynamic configuration

The previous method was to predefine the configuration file. In the specific operation method, we can still dynamically configure some parameters (or add new configurations ), it mainly refers to parameters that are not used yet.

Set the new value:C ('parameter name', 'parameter data ') 

For example, you can use

// Dynamically change the cache validity period C ('data _ CACHE_TIME ', 60); // obtain the set parameter value C ('user _ CONFIG. USER_TYPE '); // set the new value C ('user _ CONFIG. USER_TYPE ', 1 );

  

 

5. Extended Configuration

The extended configuration supports automatic loading of additional custom configuration files, and the configuration format is the same as that of the project configuration. You can set the extension configuration as follows (multiple files are separated by commas ):

// LOAD the extension configuration file 'Load _ EXT_CONFIG '=> 'user, db ',

Suppose the Extension Configuration Fileuser.phpAnddb.phpIt is used for user configuration and database configuration respectively. The advantage of this is that even if the debugging mode is disabled in the future, the database configuration file will automatically take effect after you modify it.

If it is configured in the public configuration file of the application, the configuration file under the Public configuration directory of the application will be automatically loaded.Application/Common/Conf/user.phpAndApplication/Common/Conf/db.php.

If it is configured in the configuration file of the module (assuming the Home module), the configuration file under the Module Directory will be automatically loaded.Application/Home/Conf/user.phpAndApplication/Home/Conf/db.php.

By default, the configuration parameters in the extension configuration file are incorporated into the project configuration file. That is, the default parameters are all level 1 configuration parameters. For example, the configuration parameters in user. php are as follows:

<? Php // return array ('user _ type' => 2, // user type 'user _ AUTH_ID '=> 10, // USER authentication ID 'user _ AUTH_TYPE '=> 2, // USER Authentication Mode );

The final way To get user parameters is:

C('USER_AUTH_ID');

If the configuration file is changed:

// LOAD the extension configuration file 'Load _ ext_config' => array ('user' => 'user', 'db' => 'db '),

Then the read Method

C('USER.USER_AUTH_ID');

6. Batch Configuration

The C configuration method supports batch configuration, for example:

$ Config = array ('web _ SITE_TITLE '=> 'thinkphp', 'web _ SITE_DESCRIPTION' => 'open source PHP framework'); C ($ config );

Configuration parameters in the $ config array are merged into the existing global configuration.

We can read the configuration parameters in the database in this way, for example:

// Read the configuration in the database (assume there is a config table used to save the configuration parameters) $ config = M ('config')-> getField ('name, value '); // config is an associated array. The key value is the configuration parameter value. // For example, array ('config1' => 'val1 ', 'config2' => 'val2 ',...) C ($ config); // merge configuration parameters to global configuration

After merging, we can read the configuration parameters in the database just like reading common configuration parameters. Of course, the configuration parameters in the database can also be changed dynamically.

// Read the configuration parameter C ('config1') in the database merged into the global configuration; // dynamically change the configuration parameter (the current request is valid and will not be automatically saved to the database) C ('config2', 'value _ new ');

  

  

  

  

  

  

  

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.