View the source code of the CI framework-Config. php

Source: Internet
Author: User
CI framework source code reading --------- Config. php file address :. /system/core/Config. php: Managing Configuration 1. member attributes $ config list of all loaded configurations & #20540; 2. member attribute $ is_loaded: list of all loaded configuration files 3. member attributes $ _ con CI framework source code reading --------- Config. php
File address:./system/core/Config. php
Main role: manage configurations
1. member attribute $ config list of all loaded configuration values
2. member attributes $ is_loaded: list of all loaded configuration files
3. member attributes $ _ config_paths: list of search paths when the configuration file needs to be loaded
4. The _ construct () constructor will automatically execute this method first.
It mainly includes two items. a) get the configuration and assign the value to the member attribute $ config.
B) set the base_url in the configuration.
5. load ($ file = '', $ use_sections = FALSE, $ fail_gracefully = FALSE)
Load the configuration file
$ File is the name of your custom configuration file, which does not have the. php extension.
$ Use_sections if you need to load multiple custom configuration files, they are usually merged into an array. However, if an index with the same name exists in different configuration files, a conflict may occur. To avoid this problem, you can set the second parameter to TRUE, which stores the content of each configuration file in a separate array. The index of the array is the name of the configuration file.
$ Fail_gracefully allows you to block error messages generated when the configuration file does not exist:
(0) filter and set the $ file variable
(1) initialize $ founf to FALSE to determine whether the object exists.
(2) initialize $ loaded to FALSE to determine whether the file is loaded.
(3) check the file path. If an environment variable exists, add the environment variable.
(4) go to foreach to traverse the file path and check whether the file is loaded and exists.
(5) jump out of the foreach loop if the file does not exist
(6) load files
(7) determine whether $ config exists. this $ config should be defined in the loaded file.
(8) if $ use_sections is true, the content of each configuration file is stored in a separate array. The index of the array is the name of the configuration file.
If it is false, all configuration files will be merged into an array.
(9) add the file to the is_loaded array and destroy $ config
(10) $ loaded is set to true. the log record jumps out of the loop.
(11) if loaded is false and $ fail_gracefully is not equal to true, the error log is displayed.


6. item () gets a configuration item
$ Item configuration item name
$ Index if an array is configured, this item is the index name of the array.
(1) judge whether the index is null
(2) If the index is null, determine whether $ this-> config [$ item] exists. If no return exists, assign the value to $ pref.
(3) If the index is not empty, determine whether $ this-> config [$ index] exists and whether $ this-> config [$ index] [$ item] exists, assign $ this-> config [$ index] [$ item] to $ pref.
(4) $ pref returned


7. slash_item () obtain a configuration item and add/
$ Item configuration item name
(1) judge whether the configuration item exists or not and return false
(2) judge whether the configuration item is null. if it is null, return''
(3) Add/after the configuration value and return it.


8. site_url () this function obtains the URL of your website, which contains the value of "index" you set in the config file.
$ Uri the uri string is the parameter included in the access path.
(1) if $ uri = '', a url consisting of base_url and index_page is returned.
(2) determine whether the $ this-> item ('enable _ query_strings ') is true or false and return different types of addresses. (This item is configured in the application/config. php file. It is used to differentiate the parameter passing method. if it is false, it is the default parameter passing method example.com/who/what/where /. If it is true, it is example.com/index.php? C = controller & m = function .)

9. base_url () This function returns the root URL of the site. you can splice a URL path after this function to generate the URL of the CSS or image file.

10. _ uri_string () construct the uri string for use by site_url () and base_url () functions.


11. system_url () this function gets the URL of the system folder.


12. set_item () sets a configuration item


13. _ assign_to_config () configure multiple configuration items (in the form of an array, key is the name of the configuration item to be set, and value is the value of the configuration item)


Source code:

 Config = & get_config (); log_message ('debug', "Config Class Initialized "); // Set the base_url automatically if none was provided if ($ this-> config ['base _ url'] = '') {if (isset ($ _ SERVER ['http _ host']) {$ base_url = isset ($ _ SERVER ['https']) & strtolower ($ _ SERVER ['https'])! = 'Off '? 'Https': 'http'; $ base_url. = '://'. $ _ SERVER ['http _ host']; $ base_url. = str_replace (basename ($ _ SERVER ['script _ name']), '', $ _ SERVER ['script _ name']);} else {$ base_url =' http://localhost/ ';}$ This-> set_item ('base _ url', $ base_url );}} // --------------------------------/*** Load Config File ** @ accesspublic * @ paramstringthe config file name * @ param boolean if configuration values shocould be loaded into their own section * @ parboolean true if errors shoshould just return false, false if an error message shocould be displayed * @ returnbooleanif the file was loaded correctly */function l Oad ($ file = '', $ use_sections = FALSE, $ fail_gracefully = FALSE) {$ file = ($ file = '')? 'Config': str_replace ('. php', '', $ file); $ found = FALSE; $ loaded = FALSE; $ check_locations = defined ('environment ')? Array (ENVIRONMENT. '/'. $ file, $ file): array ($ file); foreach ($ this-> _ config_paths as $ path) {foreach ($ check_locations as $ location) {$ file_path = $ path. 'config /'. $ location. '. php '; if (in_array ($ file_path, $ this-> is_loaded, TRUE) {$ loaded = TRUE; continue 2;} if (file_exists ($ file_path )) {$ found = TRUE; break ;}} if ($ found === FALSE) {continue;} include ($ file_path); if (! Isset ($ config) OR! Is_array ($ config) {if ($ fail_gracefully = TRUE) {return FALSE;} show_error ('your '. $ file_path. 'File does not appear to contain a valid configuration array. ');} if ($ use_sections === TRUE) {if (isset ($ this-> config [$ file]) {$ this-> config [$ file] = array_merge ($ this-> config [$ file], $ config );} else {$ this-> config [$ file] = $ config; }} else {$ this-> config = array_merge ($ this-> config, $ config );} $ this-> is_loaded [] = $ File_path; unset ($ config); $ loaded = TRUE; log_message ('debug', 'config file loaded :'. $ file_path); break;} if ($ loaded = FALSE) {if ($ fail_gracefully = TRUE) {return FALSE;} show_error ('The configuration file '. $ file. '. php does not exist. ');} return TRUE;} // --------------------------------/*** Fetch to get a config file item *** @ accesspublic * @ paramstringthe config item name * @ paramstringt He index name * @ parambool * @ returnstring */function item ($ item, $ index = '') {if (! Isset ($ this-> config [$ item]) {return FALSE;} $ pref = $ this-> config [$ item];} else {if (! Isset ($ this-> config [$ index]) {return FALSE;} if (! Isset ($ this-> config [$ index] [$ item]) {return FALSE;} $ pref = $ this-> config [$ index] [$ item];} return $ pref;} // --------------------------------/*** Fetch a config file item-adds slash after item (if item is not empty) ** @ accesspublic * @ paramstringthe config item name * @ parambool * @ returnstring */function slash_item ($ item) {if (! Isset ($ this-> config [$ item]) {return FALSE;} if (trim ($ this-> config [$ item]) = '') {return '';} return rtrim ($ this-> config [$ item], '/'). '/';} // ------------------------------/*** Site URL * Returns base_url. index_page [. uri_string] ** @ accesspublic * @ paramstringthe URI string * @ returnstring */function site_url ($ uri = '') {if ($ uri = '') {return $ this-> slash_item ('base _ url '). $ this-> item ('index _ Page ');} if ($ this-> item ('enable _ query_strings') = FALSE) {$ suffix = ($ this-> item ('URL _ suffix ') = FALSE )? '': $ This-> item ('URL _ suffix '); return $ this-> slash_item ('base _ url '). $ this-> slash_item ('index _ page '). $ this-> _ uri_string ($ uri ). $ suffix;} else {return $ this-> slash_item ('base _ url '). $ this-> item ('index _ page '). '? '. $ This-> _ uri_string ($ uri) ;}// -----------------------/*** Base URL * Returns base_url [. uri_string] ** @ access public * @ param string $ uri * @ return string */function base_url ($ uri = '') {return $ this-> slash_item ('base _ url '). ltrim ($ this-> _ uri_string ($ uri), '/');} // -------------------------/*** Build URI string for use in Config: site_url () and Config: base_url () ** @ access protected * @ par Am $ uri * @ return string */protected function _ uri_string ($ uri) {if ($ this-> item ('enable _ query_strings ') = FALSE) {if (is_array ($ uri) {$ uri = implode ('/', $ uri) ;}$ uri = trim ($ uri ,'/');} else {if (is_array ($ uri) {$ I = 0; $ str = ''; foreach ($ uri as $ key => $ val) {$ prefix = ($ I = 0 )? '': '&'; $ Str. = $ prefix. $ key. '= '. $ val; $ I ++;} $ uri = $ str ;}} return $ uri ;} // ------------------------------/*** System URL ** @ accesspublic * @ returnstring */function system_url () {$ x = explode ("/", preg_replace ("| /*(. + ?) /* $ | "," \ 1 ", BASEPATH); return $ this-> slash_item ('base _ url '). end ($ x ). '/';} // ------------------------------/*** Set a config file item ** @ accesspublic * @ paramstringthe config item key * @ paramstringthe config item value * @ returnvoid */function set_item ($ item, $ value) {$ this-> config [$ item] = $ value ;} // --------------------------------/*** Assign to Config ** This function is called by the front controller (CodeIgniter. php) * after the Config class is instantiated. it permits config items * to be assigned or overriden by variables contained in the index. php file ** @ accessprivate * @ paramarray * @ returnvoid */function _ assign_to_config ($ items = array () {if (is_array ($ items )) {foreach ($ items as $ key => $ val) {$ this-> set_item ($ key, $ val );}}}} // END CI_Config class/* End of file Config. php * // * Location :. /system/core/Config. php */


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.