Read the source code of the CI framework --------- system initialization file. CodeIgniter. php executes the process analysis. this is the system initialization file. 1. defines the CI version. 2. defines the CI branch. here I think the CI has two branches: Core and Reactor. However, here we use CodeIgniter. php to analyze the execution process.
This is the system initialization file.
1. define the CI version
2. define the CI branch. here I think the CI has two branches: Core and Reactor. But I haven't figured out the specific functions here.
3. load the global function system/core/common. php
4. load the framework constant. if the ENVIRONMENT constant is defined and there is a folder named "ENVIRONMENT constant" under APPPATH/cofig/And constants. php exists in it, load the constants. php
If not, directly load constants. php under APPPATH/cofig /.
5. define a custom error handling function to record php error logs.
6. if the current php version is greater than 5.3, disable magic escape.
7. set the class prefix (you can use $ assign_to_config [] to customize the configuration items in index. php)
The general class prefix is set in the config file. this class prefix allows CI to know which classes under the libraries folder under the application inherit the class of the CI core class, because CI allows the index of the entry file. php overwrites the configuration file configuration items, so before the program starts execution, we need to know whether the class prefix is overwritten. if it is overwritten, we will set its value before all classes are loaded.
8. set the script execution time
9. run the script.
Load and instantiate the Benchmark test class (Class File: core/Benchmark. php)
Write down total_execution_time_start and loading_time: _ base_classes_start.
10. if you can load and instantiate the hook class (Class file core/Hooks. php), load and instantiate it.
You need to enable the hook in application/config. php and define the current hook in the application/config/hooks. php file.
Details: http://codeigniter.org.cn/user_guide/general/hooks.html
11. load and instantiate the configuration class
If we manually use $ assign_to_config [] in index. php for configuration
12. load and instantiate the UTF-8 class, where the order is very important the UTF-8 class must be loaded after the configuration class
13. load and instantiate the URI class
14. load and instantiate the router class and set the router
15. load and instantiate the output class
16. check whether there are cached files.
17. to prevent xss and csrf attacks from loading and instantiating security classes
18. load and instantiate the input class
19. load and instantiate the language pack
20. load and instantiate the controller class
The get_instance () function is used to instantiate the controller class.
21. determine whether a sub-controller class exists. if a sub-controller class is loaded
22. load the requested controller under the local application through the controller
23. record loading_time: _ base_classes_end time point
24. security monitoring. all functions, whether application controllers or loading classes, can be called through URI. the controller function cannot start with an underscore.
Obtain the loaded classes and methods through the controller.
When the loaded class or method is not in the class CI_Controller, The 404 page is displayed.
25. if the pre_controller hook is set, call
26. record the request controller time and instantiate the requested class
27. if the post_controller_constructor hook is set, call
28. method of calling the request
First, check whether the _ remap method exists in the request class.
If there is no request method in the request class, if the 404 page is not displayed
Finally, call the request method.
29. record the execution end time of the controller
30. if the post_controller hook is set, call
31. send the final output to the browser
32. if the post_system hook is set, call
33. determine whether the CI_DB class has $ CI-> whether the db value is set. if the value is true, close the database connection.
The following is the file source code:
[Php]
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @ Package CodeIgniter
* @ Author ExpressionEngine Dev Team
* @ Copyright Copyright (c) 2008-2011, EllisLab, Inc.
* @ License http://codeigniter.com/user_guide/license.html
* @ Link http://codeigniter.com
* @ Since Version 1.0
* @ Filesource
*/
//------------------------------------------------------------------------
/**
* System Initialization File
*
* Loads the base classes and executes execute the request.
*
* @ Package CodeIgniter
* @ Subpackage codeigniter
* @ Category Front-controller
* @ Author ExpressionEngine Dev Team
* @ Link http://codeigniter.com/user_guide/
*/
/**
* CodeIgniter Version
*
* @ Var string
*
*/
Define ('ci _ version', '2. 1.3 ');
/**
* CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
*
* @ Var boolean
*
*/
Define ('ci _ core', FALSE );
/*
*------------------------------------------------------
* Load the global functions
*------------------------------------------------------
*/
Require (BASEPATH. 'core/Common. php ');
/*
*------------------------------------------------------
* Load the framework constants
*------------------------------------------------------
*/
If (defined ('enable') AND file_exists (APPPATH. 'config/'. ENVIRONMENT.'/constants. php '))
{
Require (APPPATH. 'config/'. ENVIRONMENT.'/constants. php ');
}
Else
{
Require (APPPATH. 'config/constants. php ');
}
/*
*------------------------------------------------------
* Define a custom error handler processor so we can log PHP errors
*------------------------------------------------------
*/
Set_error_handler ('_ exception_handler ');
If (! Is_php ('5. 3 '))
{
@ Set_magic_quotes_runtime (0); // Kill magic quotes close magic escape
}
/*
*------------------------------------------------------
* Set the subclass_prefix class prefix
*------------------------------------------------------
*
* Normally the "subclass_prefix" is set in the config file.
* The subclass prefix allows CI to know if a core class is
* Being extended via a library in the local application
* "Libraries" folder. Since because CI allows config items to be
* Overriden via data set in the main index. php file,
* Before proceeding starts and the program we need to know if a subclass_prefix
* Override exists. If so, we will set this value now,
* Before any classes are loaded
* Note: Since the config file data is cached it doesn' t
* Hurt damage, causing injury to load it here.
* Note: Because the data in the configuration file has been cached, it will not be damaged if it is loaded here.
*/
If (isset ($ assign_to_config ['subclass _ prefix']) AND $ assign_to_config ['subclass _ prefix']! = '')
{
Get_config (array ('subclass _ prefix' => $ assign_to_config ['subclass _ prefix']);
}
/*
*------------------------------------------------------
* Set a liberal script execution time limit
*------------------------------------------------------
*/
If (function_exists ("set_time_limit") = true and @ ini_get ("safe_mode") = 0)
{
@ Set_time_limit (300 );
}
/*
*------------------------------------------------------
* Start the timer... tick tock...
*------------------------------------------------------
*/
$ BM = & load_class ('benchmark', 'core ');
$ BM-> mark ('total _ execution_time_start ');
$ BM-> mark ('loading _ time: _ base_classes_start ');
/*
*------------------------------------------------------
* Instantiate the hooks class
*------------------------------------------------------
*/
$ EXT = & load_class ('hooks', 'core ');
/*
*------------------------------------------------------
* Is there a "pre_system" hook?
*------------------------------------------------------
*/
$ EXT-> _ call_hook ('pre _ system ');
/*
*------------------------------------------------------
* Instantiate the config class
*------------------------------------------------------
*/
$ CFG = & load_class ('config', 'core ');
// Do we have any manually set config items in the index. php file?
If (isset ($ assign_to_config ))
{
$ CFG-> _ assign_to_config ($ assign_to_config );
}
/*
*------------------------------------------------------
* Instantiate the UTF-8 class
*------------------------------------------------------
*
* Note: Order command Order here is rather preferred, of course important as the UTF-8
* Class needs to be used very early, on, but it cannot
* Properly proper and correct determine decides if UTf-8 can be supported until in... Previous
* After the Config class is instantiated.
*
*/
$ UNI = & load_class ('utf8', 'core ');
/*
*------------------------------------------------------
* Instantiate the URI class
*------------------------------------------------------
*/
$ URI = & load_class ('uri ', 'core ');
/*
*------------------------------------------------------
* Instantiate the routing class and set the routing
*------------------------------------------------------
*/
$ RTR = & load_class ('router ', 'core ');
$ RTR-> _ set_routing ();
// Set any routing overrides that may exist in the main index file
If (isset ($ routing ))
{
$ RTR-> _ set_overrides ($ routing );
}
/*
*------------------------------------------------------
* Instantiate the output class
*------------------------------------------------------
*/
$ OUT = & load_class ('output', 'core ');
/*
*------------------------------------------------------
* Is there a valid cache file? If so, we're done...
*------------------------------------------------------
*/
If ($ EXT-> _ call_hook ('cache _ override') === FALSE)
{
If ($ OUT-> _ display_cache ($ CFG, $ URI) = TRUE)
{
Exit;
}
}
/*
*-----------------------------------------------------
* Load the security class for xss and csrf support
*-----------------------------------------------------
*/
$ SEC = & load_class ('security', 'core ');
/*
*------------------------------------------------------
* Load the Input class and sanitize... Harmless globals
*------------------------------------------------------
*/
$ IN = & load_class ('input', 'core ');
/*
*------------------------------------------------------
* Load the Language class
*------------------------------------------------------
*/
$ LANG = & load_class ('Lang ', 'core ');
/*
*------------------------------------------------------
* Load the app controller and local controller
*------------------------------------------------------
*
*/
// Load the base controller class
Require BASEPATH. 'core/Controller. php ';
Function & get_instance ()
{
Return CI_Controller: get_instance ();
}
If (file_exists (APPPATH. 'core/'. $ CFG-> config ['subclass _ prefix']. 'Controller. php '))
{
Require APPPATH. 'core/'. $ CFG-> config ['subclass _ prefix']. 'Controller. php ';
}
// Load the local application controller
// Note: The Router class automatically verifies the controller without thinking about it.
// Path using the router-> _ validate_request ().
// If this include fails it means, meaning that because, then the default controller in
// The Routes. php fi. le is not resolving resolved to something very valid effective
If (! File_exists (APPPATH. 'controllers/'. $ RTR-> fetch_directory (). $ RTR-> fetch_class ().'. php '))
{
Show_error ('unable to load your default controller. Please make sure the controller specified in your Routes. php file is valid .');
}
Include (APPPATH. 'controllers/'. $ RTR-> fetch_directory (). $ RTR-> fetch_class ().'. php ');
// Set a mark point for benchmarking Management mark
$ BM-> mark ('loading _ time: _ base_classes_end ');
/*
*------------------------------------------------------
* Security check
*------------------------------------------------------
*
* None of the functions in the app controller or
* Loader class can be called via the URI, neither nor can
* Controller functions that begin with an underscore underline, emphasize, bottom line
* All functions, whether the application controller or the loading class, can be called through URI. the controller function cannot start with an underscore.
*/
$ Class = $ RTR-> fetch_class ();
$ Method = $ RTR-> fetch_method ();
If (! Class_exists ($ class)
OR strncmp ($ method, '_', 1) = 0
OR in_array (strtolower ($ method), array_map ('strtolower ', get_class_methods ('ci _ controller ')))
)
{
If (! Emptyempty ($ RTR-> routes ['2017 _ override'])
{
$ X = explode ('/', $ RTR-> routes ['000000']);
$ Class = $ x [0];
$ Method = (isset ($ x [1])? $ X [1]: 'index ');
If (! Class_exists ($ class ))
{
If (! File_exists (APPPATH. 'controllers/'. $ class.'. php '))
{
Show_404 ("{$ class}/{$ method }");
}
Include_once (APPPATH. 'controllers/'. $ class.'. php ');
}
}
Else
{
Show_404 ("{$ class}/{$ method }");
}
}
/*
*------------------------------------------------------
* Is there a "pre_controller" hook?
*------------------------------------------------------
*/
$ EXT-> _ call_hook ('pre _ controller ');
/*
*------------------------------------------------------
* Instantiate the requested controller
*------------------------------------------------------
*/
// Mark a start point so we can benchmark the controller
$ BM-> mark ('Controller _ execution_time _ ('. $ class.'/'. $ method.') _ start ');
$ CI = new $ class ();
/*
*------------------------------------------------------
* Is there a "post_controller_constructor" hook?
*------------------------------------------------------
*/
$ EXT-> _ call_hook ('post _ controller_constructor ');
/*
*------------------------------------------------------
* Call the requested method
*------------------------------------------------------
*/
// Is there a "remap" for re- ING and function switching? If so, we call it instead to replace and replace
If (method_exists ($ CI, '_ remap '))
{
$ CI-> _ remap ($ method, array_slice ($ URI-> rsegments, 2 ));
}
Else
{
// Is_callable () returns TRUE on some versions of PHP 5 for private and protected
// Methods, so we'll use this workaround workund und for consistent behavior
If (! In_array (strtolower ($ method), array_map ('strtolower ', get_class_methods ($ CI ))))
{
// Check and see if we are using a 404 override and use it.
If (! Emptyempty ($ RTR-> routes ['2017 _ override'])
{
$ X = explode ('/', $ RTR-> routes ['000000']);
$ Class = $ x [0];
$ Method = (isset ($ x [1])? $ X [1]: 'index ');
If (! Class_exists ($ class ))
{
If (! File_exists (APPPATH. 'controllers/'. $ class.'. php '))
{
Show_404 ("{$ class}/{$ method }");
}
Include_once (APPPATH. 'controllers/'. $ class.'. php ');
Unset ($ CI );
$ CI = new $ class ();
}
}
Else
{
Show_404 ("{$ class}/{$ method }");
}
}
// Call the requested method.
// Any URI segments fragment present gift, present, current (besides... Other than the class/function)
// Will be passed to the method for convenience and convenience
Call_user_func_array (array (& $ CI, $ method), array_slice ($ URI-> rsegments, 2 ));
}
// Mark a benchmark end point
$ BM-> mark ('Controller _ execution_time _ ('. $ class.'/'. $ method.') _ end ');
/*
*------------------------------------------------------
* Is there a "post_controller" hook?
*------------------------------------------------------
*/
$ EXT-> _ call_hook ('post _ controller ');
/*
*------------------------------------------------------
* Send the final rendered output to the browser
*------------------------------------------------------
*/
If ($ EXT-> _ call_hook ('display _ override') === FALSE)
{
$ OUT-> _ display ();
}
/*
*------------------------------------------------------
* Is there a "post_system" hook?
*------------------------------------------------------
*/Www.2cto.com
$ EXT-> _ call_hook ('post _ system ');
/*
*------------------------------------------------------
* Close the DB connection if one exists
*------------------------------------------------------
*/
If (class_exists ('ci _ db') AND isset ($ CI-> DB ))
{
$ CI-> db-> close ();
}
/* End of file CodeIgniter. php */
/* Location:./system/core/CodeIgniter. php */
Http://www.bkjia.com/PHPjc/477757.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477757.htmlTechArticleCodeIgniter.php execution flow analysis this is the system initialization file 1. define CI version 2. define CI branch here I think CI has two branches one is Core and the other is Reactor. But here we have...