PHPCodeIgniter framework source code analysis

Source: Internet
Author: User
1. index. php: the entry file define (ENVIRONMENT) is mainly used to set the errors log output level -- $ system_path to set the system path -- set BASEPATH, FCPATH, SYSDIR, APPPATH, and so on | sets the path information variable, prepare for loading the corresponding file information -- require_onceBASEPATH.coreCodeIgniter.php | last

1. index. php: the entry file -- define ('Environment ') is mainly used to set the errors log output level -- $ system_path to set the system path -- set BASEPATH, FCPATH, SYSDIR, and APPPATH | set the path information variable, prepare -- require_once BASEPATH for loading the corresponding file information. core/CodeIgniter. php | last

1. index. php: entry file | --> define ('enable ') | mainly used to set the errors log output level | --> $ system_path | set the system path | --> set BASEPATH, FCPATH, SYSDIR, and APPPATH | set the path information variable, prepare for loading the corresponding file information | --> require_once BASEPATH. core/CodeIgniter. php | finally load CodeIgniter. php as the controller 2. codeIgniter. php is mainly used to load the corresponding files in the core directory of the core. | --> require (BASEPATH. 'Core/Common. php '); | loads the Common file under the core directory. For details, see "2.1 parsing | --> require (APPPATH. 'config /'. ENVIRONMENT. '/constants. php '); | load the constants object Recording, use the constants directory in the config directory if it is not related to the development environment | --> get_config (array ('subclass _ prefix' => $ assign_to_config ['subclass _ prefix']); | sets the subfile and prefix of the extension class | --> $ BM = & load_class ('benchmark', 'core'); | loads the Benchmark class, mark records the current time | --> $ EXT = & load_class ('hooks', 'core '); | load the Hooks hook class under the core directory | --> $ EXT-> _ call_hook ('pre _ system'); | call _ call_hook (pre_system ), execute the hook by calling _ run_hook in pre_system and preprocessing before the system starts to work. | --> $ CFG = & load_class ('config', 'c Ore '); | continue to execute the Config configuration file under the core, | --> $ CFG-> _ assign_to_config ($ assign_to_config ); | --> | $ this-> set_item ($ key, $ val); | parses the configuration file specified to config, essentially assign a value to config [] | --> $ UNI = & load_class ('utf8', 'core'); | loads the UTF-8 encoding class, CI_Utf8 | --> $ URI = & load_class ('uri ', 'core'); | URI class for loading the core directory, CI_URI | --> $ RTR = & load_class ('router ', 'core'); | sets route and overwrite information for route. For details, see section 2.2. | --> _ set_routing () | --> _ set_overrides () | --> $ OUT = & load_class ('output', 'c Ore '); | instantiate the output class and load the output file under the core directory | --> $ OUT-> _ display_cache ($ CFG, $ URI) | determine whether the page cache exists, yes, the output file | --> $ SEC = & load_class ('security', 'core '); | load the Security processing file under the core directory | --> $ IN = & load_class ('input', 'core'); | instantiate the Input class, load the input file in the core directory | --> $ LANG = & load_class ('lang ', 'core'); | load the language class | --> require BASEPATH. 'Core/Controller. php'; | load the basic controller class. For details, see Section 2.3. | --> require APPPATH. 'Core /'. $ CFG-> config ['subclass _ prefix']. 'controller. php'; | Try to load the extended custom subclass controller | --> include (APPPATH. 'controllers /'. $ RTR-> fetch_directory (). $ RTR-> fetch_class (). '. php '); | loads the Controller class under the custom controller | --> $ BM-> mark ('loading _ time: _ base_classes_end '); | set a benchmark test point | --> $ class = $ RTR-> fetch_class (); | controller class name and method name for obtaining uri addresses respectively | --> $ method = $ RTR-> fetch_method (); | --> if (! Class_exists ($ class) | judge whether the methods and classes are reasonable OR strncmp ($ method, '_', 1) = 0OR in_array (strtolower ($ method ), array_map ('strlower ', get_class_methods ('Ci _ controller') | --> $ EXT-> _ call_hook ('pre _ controller '); | pre-process the processor before execution and set the benchmark | --> $ CI = new $ class (); | obtains the Controller instance for execution, instantiate the constructor | --> $ EXT-> _ call_hook ('Post _ controller_constructor '); | hook processing after instantiating the Controller class | --> if (method_exists ($ CI, '_ remap') | --> $ CI-> _ remap ($ method, array_slice ($ URI-> rsegments, 2) | if the Controller has the _ remap () method, run the command. The judgment condition $ CI is the Controller class. | --> else | the Judgment Method in the class seems to exist. If not, set | --> call_user_func_array (array (& $ CI, $ method), array_slice ($ URI-> rsegments, 2 )); | the final parameter is passed for calling the control method. | --> $ BM-> mark ('controller _ execution_time _('. $ class. '/'. $ method. ') _ end'); | benchmark end Time | --> $ EXT-> _ call_hook ('Post _ controller'); | controller lifecycle, perform subsequent operations after the Controller completes | --> $ OUT-> _ display (); | display the output page | --> $ EXT-> _ call_hook ('Post _ system '); | end operation after the request lifecycle is completed | --> $ CI-> db-> close (); | automatically closes database resource 2.1 Core/Common. php loading | --> function is_php ($ version) | a function used to compare the version number | --> function is_really_writable ($ file) | used to determine whether a file can be written, reliability varies in different systems. In W, is_readonly is determined. In U, if safe_mode is enabled, uncertainty is obtained. | --> function load_class ($ class, $ directory = 'libraries ', $ prefix = 'Ci _ ') | class used to load the PHP file in the directory | --> foreach (array (APPPATH, BASEPATH) as $ path) | perform the next cycle in the application and system directories. | --> file_exists ($ path. $ directory. '/'. $ class. '. php' | find the corresponding php file | --> require ($ path. $ directory. '/'. $ class. '. php '); | require loads the class in the corresponding php file with a prefix, which can be expanded here | --> break; | exit if the file is loaded correctly, otherwise, try to load the file again. | --> file_exists (APPPATH. $ directory. '/'. config_item ('subclass _ prefix '). $ class. '. php ') | self-extended class, such as My_Test | --> if ($ name = FALSE) | exit () if $ name does not exist, (When a custom class is loaded, this can be used as an extension point to add marginal conditions) | --> is_loaded ($ class ); | indeed the class has been loaded | --> $ _ classes [$ class] = new $ name (); | load to the static classes ancestor for caching, obtain from classes when calling | --> function is_loaded ($ class = '') | --> sets the $ _ is_loaded ancestor. The parameter $ class is not empty, determine whether gf $ _ is_loaded exists; otherwise, it is set | --> function & get_config ($ replace = array () | used to obtain the instantiated Config file | --> static $ _ config; | define the config type | --> $ file_path = APPPATH. 'config/config. php'; | determine the config defined under the application directory path. php path | --> require ($ file_path); | load application/config. php class | --> count ($ replace)> 0 | for config. variables defined in php are replaced one by one if replace exists. | --> foreach ($ replace as $ key => $ val) | --> $ config [$ key] = $ val; | --> return $ _ config [0] = & $ config; | Finally, return the result set of the defined config | --> function config_item ($ item) | configuration options, return special configuration items from the config's ancestor object | --> $ config = & get_config (); | --> $ _ config_item [$ item] = $ config [$ item]; | --> function show_error | output of error information | --> $ _ error = & load_class ('exception', 'core '); | load Exceptions class | --> echo $ _ error-> show_error ($ heading, $ message, 'error _ general ', $ status_code ); | direct output error | --> function show_404 | used to output the 404 page, the output error message page can be configured. | --> function log_message | used to write log information | --> $ _ Log = & load_class ('log '); | --> $ _ log-> write_log ($ level, $ message, $ php_error ); | --> function set_status_header | heade information used to output status | --> function _ exception_handler | --> function remove_invisible_characters | --> function html_escape | filter HTML variables | --> return htmlspecialchars ($ var, ENT_QUOTES, config_item ('charset'); 2.2Router route information Settings | --> _ set_routing () | --> $ segments = array () | according to the directory, controller, the trigger of the function sets the uri segment value of the segment [], respectively. The fetch () method gets the object Value | --> include (APPPATH. 'config/routes. php '); | loads the routes file under config | --> $ this-> routes | sets the routes ancestor value, GET | --> $ this-> default_controller | set the Controller value of routes, GET | --> return $ this-> _ validate_request ($ segments) from the config route ); | verify the validity of the uri's segment | --> $ this-> uri-> _ remove_url_suffix (); $ this-> uri-> _ reindex_segments (); | further clears the parsing uri, make segment start from 1 x | --> _ set_overrides () | according to the value of $ routing, reset the directory, controller, and function parameters. | --> $ this-> set_directory ($ routing ['directory']); | --> $ this-> set_class ($ routing ['controller']); | --> $ this-> set_method ($ routing ['function']); 2.3 core/Controller. php loading | -- >__ construct () | constructor | --> self: $ instance = & $ this; | --> foreach (is_loaded () as $ var => $ class) | loads the corresponding class based on is_loaded () | --> $ this-> $ var = & load_class ($ class ); | --> $ this-> load = & load_class ('loader ', 'core '); | load the PHP file of core/Loader | --> $ this-> load-> initialize (); | mainly used for autoload loading information, such as libraries and database | --> function & get_instance | returns the current instance | --> return self: $ instance extension point: Application of PHP automatic loading mechanism in CodeIgniter 1. PHP automatic loading mechanism: After PHP5, the automatic loading mechanism of the class is provided, that is, the class is used only during loading, that is, Lazy loading. There are two ways: 1.1: _ autoload () you can achieve this through extension. In essence, it is to set the rules to load the PHP file of the corresponding path (require, include mode) 1.2: Point autoload_func to the PHP file. For details, refer to the C language extension implementation: http://blog.csdn.net/flyingpig4/article/details/72864382 . According to the above source code analysis, the application in CodeIgniter shows that all the operations in CodeIgniter start with Controller. You only need to make the _ autoload () function automatically load during Cotroller loading, the current loading method is in application/config. set the _ autoload () function in php.

Original article address: PHP CodeIgniter framework source code parsing. Thank you for sharing it with me.

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.