_php example of Read and C method in thinkphp configuration

Source: Internet
Author: User
Tags mixed php template codeigniter zend framework

The examples in this article describe the read and C methods configured in thinkphp. Share to everyone for your reference, specific as follows:

1. Project Public Configuration

conf/config.php

The contents are as follows

<?php
/** *
 Project public configuration
 * @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 compression output
 ' page_num '    =>,
 /*cookie configuration * *
 ' Cookie_path '   => '/',    //cookie path
 ' cookie_prefix '   => ',    //Cookie prefix avoidance conflict/
 * Define Template Label * * *
 tmpl_l_delim '   => ' {sh: ',   ///Templates engine normal label start tag
 ' Tmpl_r_delim '   => '} ',    // Template engine normal tag end tag
 ' tmpl_cache_on '   => false,   //close template Cache
 ' Default_group '   => ' home ',   / /default Access group, set default entry
 ' app_group_list '  => ' Agent,home,system,user,store,wap,mall,opener ',/  /project group set, Multiple groups are separated by commas, such as ' 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 of the load, These configurations can be read by the C () method and are globally valid.

2. If module grouping is enabled, you can define the profile separately for each grouping, which is located at:

Project configuration directory/grouping name/config.php

' App_group_list ' => ' home,admin ',//project group set
' Default_group ' => ' home ',//default group

Now that the home and admin two groupings are defined, we can define the grouping configuration file as follows:

conf/home/config.php
conf/admin/config.php

The configuration files for each grouping are valid only for the current group, and the definition format of the group configuration is the same as the project configuration.

Note: Grouping names are case-sensitive and must be consistent with the defined grouping names.

3. Read configuration

After you have defined the configuration file, you can use the C method provided by the system (if you feel strange, you can help memory by using the Config word) to read the existing configuration

C (' parameter name ')//get the parameter value that has been set

For example, C (' App_status ') can read the setting values of the system's debug mode, and because the configuration parameters are case-insensitive, C (' App_status ') is equivalent, but it is recommended that the specification of the capitalization method be used.

Returns null if the setting is not already present in App_status.

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

C (' User_config. User_type ')//Get user type settings in User Configuration

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

If the parameter is not available, all valid configurations will be read.

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

For example:

' Html_cache_time ' => 60,//Static Cache validity (sec)
' html_cache_time ' => 80, 

The final acquisition was 80.

Load order according to parameter Load_ext_config

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

For example, the info has a parameter Html_cache_time 60, the rest of the configuration is not, then this parameter read out is 60.

If the DB also has a html_cache_time of 50, then the value is 60. Since info is read later, the Html_cache_time in DB is overwritten.

Attach C method source code

/** * Get and set configuration parameters support Batch definition * @param string|array $name configuration variable * @param mixed $value configuration value * @return Mixed/function C ($nam
 E=null, $value =null) {Static $_config = array (); Gets all if (empty ($name)) {if!empty ($value) && $array = cache (' c_ '. $value)) {$_config = Array_merge ($
  _config, Array_change_key_case ($array));
 return $_config; The///precedence execution setting Gets or assigns an 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
  The///two D array settings and get support $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
  }//Batch set if (Is_array ($name)) {$_config = Array_merge ($_config, Array_change_key_case ($name));
  if (!empty ($value)) {//Save configuration Value cache (' C_ '. $value, $_config); }
  Return return null;

 Avoid illegal parameters}

More interested readers of thinkphp related content can view the website topic: "thinkphp Introductory Course", "thinkphp template Operation Skill Summary", "thinkphp Common Method Summary", "The CodeIgniter Introductory Course", "CI ( CodeIgniter) Framework Advanced tutorials, introductory tutorials for the Zend framework Framework, Smarty Templates Primer tutorial, and PHP template technology summary.

I hope this article will help you with the PHP program design based on thinkphp framework.

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.