CI source code analysis (1)-config configuration file module, ciconfig

Source: Internet
Author: User

CI source code analysis (1)-config configuration file module, ciconfig

(1) Usage
(A) system-level configuration language, character encoding, session, cookie, and other configuration items

  • File Location: application/config. php
  • Loading Method: automatic loading
  • Call method: $ this-> config-> item ('config _ item_key ');

(B) configuration items used in the actual business at the business level, or constants, such as 1 <-> male 2 <-> female 3 <-> unknown data. We strongly recommend that you separate business-level configuration items from system configuration items. CI supports multi-level directory configuration file loading. Therefore, you can create a special service configuration folder under the application/config folder, such as constants.
  • File Location: application/config/path/to/config_file.php
  • Loading Method: manually load (or in autoload. php configuration is automatically loaded ). $ this-> config-> load ('path/to/config_file ', true ). the second parameter groups the configurations of different files. We strongly recommend that you add the parameter.
  • Call method: $ this-> config-> item ('config _ item_key ', 'path/to/config_file'); the second parameter is the group specified during loading.

(C) Dynamic Modification of configuration CI all the configuration items are loaded at a time, and also supports dynamic modification of configuration items. But does not support the configuration modification of the specified group for the moment, I implemented it myself, address: https://github.com/wkupaochuan/analysis-about-ci-frame.git, path is application/core/MY_Config.php
  • Single modification: $ this-> config-> set_item ('config _ item_key ', $ value );
  • Batch modification: $ this-> config-> _ assign_to_config ($ array );


(2) Implementation Method
(A) The Common. php file is a globally callable method. The get_config method and config_item function are related to config, which are mainly used in the Framework file of system.
  • Get_config: The application/config. php file in the require file is saved in the local static variable $ _ config, so it is also loaded at a time. Note that the required configuration file config. php is loaded, so require is used for error handling.
  • Config_item: Get the loaded configuration item



(B) system/core/config. the php class is instantiated as an attribute of the super class CI $ this-> config, the main method is _ construct (), load (), item (), after the configuration file is loaded, the attribute $ config is assigned to the config object. Because the superclass CI is global, the configuration file is also loaded once.
  • _ Construct: The application/config. php file is loaded by default, and the get_config function in the Common. php file is called.
  • Load: Find the target configuration file in config/ENVIRONMENT/$ file and config/$ file Based on the input parameter $ file, and load the configuration file in the current ENVIRONMENT preferentially. After finding the configuration file, the corresponding configuration array merge will be added to the loaded configuration. Note that once two different configuration files have the same configuration key and no configuration item group is specified, the previously loaded configuration item will be overwritten. Therefore, we strongly recommend that you, the second parameter use_section is specified in each load configuration file.
  • Config_item: return the loaded configuration items. If no configuration items are found (indeed no configuration items and unloaded configuration items), false is returned.

(3) advantages and disadvantages
(A) Disadvantages
  • Every time you use $ this-> config-> item () to obtain the configuration item, it is possible that the corresponding configuration file has not been loaded, so we recommend that you load then item, or all configuration files are configured with autoload. Here, I implemented a my_item method my_item ($ file, $ item), which does not need to be loaded before calling. The method is called first. If no result is returned, the method is loaded once. Then, the method is called again and the result is returned. This simplifies the code to a certain extent and does not require autoload configuration. The path is application/core/MY_Config.php.

(B) Advantages
  • Supports directory classification for easy and clear organization and management of business configuration files



Ci framework source code analysis address https://github.com/wkupaochuan/analysis-about-ci-frame.git

Related Article

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.