The working principle of PHP CodeIgniter framework, CodeIgniter framework
CodeIgniter (hereinafter referred to as CI, official website and China Station) is a popular PHP framework, small but powerful, simple and lightweight and has a good extensibility, is also more popular in the country. CI, on the other hand, does not advance with the times and does not support some of the features behind PHP5.3, which makes it relatively more suitable for older projects. Nevertheless, CI is still an excellent framework, and it has a small kernel, elegant source code, suitable for learning.
CI is easy to use and can be easily developed for Web applications. Let's take a look at the CI Workflow flowchart (where the content is referenced from http://codeigniter.org.cn/user_guide/overview/appflow.html)
、
1.index.php as the front-end controller, initializes the basic resources required to run the CodeIgniter.
2.Router checks the HTTP request to determine who will handle the request.
3. If the cache file exists, it will bypass the usual order of system execution and be sent directly to the browser.
4. Safety (security). The HTTP request and any user-submitted data will be filtered before the application controller (application controllers) is loaded.
5. The controller loads the model, core library, auxiliary functions, and any other resources required to process a particular request.
6. Final view renders the content that is sent to the Web browser. If the cache is turned on (Caching), the view is first cached, so it will be available for future requests.
The above gives a general flow. So how does it work inside the program when it sees the page in the browser?
The following sequence of executions lists the main files loaded by the CI framework, with a brief description of their role:
index.php.
Define usage environment (environment), Frame Path (system_path,basepath), application directory (application_folder), Application path (APPPATH), etc., load (require) CI core file
basepath/core/codeigniter.php.(Ps. Actually basepath contains the final file delimiter '/', where additional '/' is for clearer presentation)
The system initialization file, the core part of the entire framework, loads a series of base classes here, and executes the request
basepath/core/common.php.
The common file contains a series of basic and public functions for global use, such as Load_class (), get_config (), etc.
Basepath/core/benchmark.
This is a benchmark class that, by default, marks the execution point of each stage of the application to get its execution time. Also allows you to define your own monitoring points.
basepath/core/hooks.php.
Ci_hooks is a hook class that is the core of the framework to be expanded to insert hook points at various stages of the program, execute your custom classes, functions, etc.
basepath/core/config.php.
Profile Management class, load read or set configuration
basepath/core/uri.php, basepath/core/router.php
The URI class helps you parse the URI of the request and provides a collection of functions that divide the URI for use by the router class
basepath/core/router.php.
A routing class that distributes user requests to a specified handler (typically an action function in a controller instance) by requesting a URI, and a user-configured route (apppath/config/routes.php)
basepath/core/output.php, basepath/core/input.php
The input class, which is the input parameter that processes the request, provides a secure way to get it. The output class sends the final execution result, and it is responsible for caching the function
Ten. basepath/core/controller.php
Controller base class, using singleton mode to provide an example to the entire application of the heart. It is a super Object, and it is important that classes loaded within an application can become member variables of the controller, which you will continue to talk about later.
apppath/controllers/$RTR->fetch_directory (). $RTR->fetch_class (). Php
Through the routing function, get the controller name, instantiate the Real controller class (subclass)
basepath/core/loader.php.
Ci_loader is used to load various class libraries, models, views, databases, files, etc. in the application and set the member variables to become controllers
Call_user_func_array calling a handler function
By routing, the action function name is obtained, the controller->action () function is called, the application logic is processed, and the actual business processing logic is written in the action function.
$OUT->_display () to output the content
These are the most basic processing processes for the entire application. The following selection of core content code to explain, to enhance the understanding of CI:
Benchmark,hooks,config in the <?php//*basepath/system/core/common.php//boot file are all functions loaded by this function &load_class ($ class, $directory = ' libraries ', $prefix = ' ci_ ') {//record the loaded class static $_classes = Array (),//have been loaded, read directly and return if (Isset ($_classes [$class])) {return $_classes[$class];} $name = false;//in the specified directory looking for the class to be loaded foreach (Array (APPPATH, basepath) as $path) {if (File_exists ($path. $directory. ') /'. $class. '. php ') {$name = $prefix. $class; if (class_exists ($name) = = = FALSE) {require ($path. $directory. ' /'. $class. '. php ');} Break;}} Failed to find if ($name = = = FALSE) {exit (' Unable to locate the specified class: '. $class. php ');} Trace the class that was loaded just now, is_loaded () function below is_loaded ($class), $_classes[$class] = new $name (); return $_classes[$class];} Record classes that have already been loaded. The function returns all Loaded class function &is_loaded ($class = ") {Static $_is_loaded = Array (), if ($class! =") {$_is_loaded[strtolower ($ Class)] = $class;} return $_is_loaded;} *basepath/system/core/controller.phpclass Ci_controller {private static $instance;p ublic function __construct () { Self:: $instanCE =& $this;//codeigniter.php all Class objects initialized in the boot file (that is, the steps just 4,5,6,7,8,9),//register as a member variable of the Controller class, making the Controller a Super object (super Object) foreach (is_loaded () as $var = = $class) {$this, $var =& load_class ($class);}Load the loader object, and then use the loader object to load a series of resources within the program$this->load =& load_class (' Loader ', ' core '); $this->load->initialize (); Log_message (' Debug ', " Controller Class Initialized ");} This function externally provides a single instance of the controller public static function &get_instance () {return self:: $instance;}} *basepath/system/core/codeigniter.php//Load the base controller Classrequire basepath. ' core/controller.php ';// Through this global function to get the example of the controller, the Super object,//means to call this function elsewhere in the program, you can get the entire frame control function &get_instance () {return Ci_controller:: Get_instance ();} Load the corresponding Controller class//Note: The Router class will automatically use Router->_validate_request () to verify the controller path if (! file_exists (APPPATH. ' controllers/'). $RTR- >fetch_directory (). $RTR->fetch_class (). php ') {show_error (' Unable to load your default controller. Sure the controller specified in your routes.php file is valid. ');} Include (APPPATH. ' controllers/'. $RTR->fetch_directory (). $RTR->fetch_class (). php '); $class = $RTR->fetch_class (); Controller class Name$method = $RTR->fetch_method (); Action name//.....//calls the requested function//URI in addition to Class/functionThe segment is also passed to the calling function Call_user_func_array (array (& $CI, $method), Array_slice ($URI->rsegments, 2));//Output the final content to the browser if ($ Ext->_call_hook (' display_override ') = = = FALSE) {$OUT->_display ();} *basepath/system/core/loader.php//See an example of a Loader class loading model. Only part of the Code public Function model ($model, $name = "", $db _conn = FALSE) {$CI =& get_instance () is listed here, if (Isset ($CI-a $name)) {Show_error (' The model name loading is the ' the name ' of the ' A resource, ' is already being used: '. $name);} $model = Strtolower ($model);//Match according to Path of the model class, if found, load foreach ($this->_ci_model_paths as $mod _path) {if (! file _exists ($mod _path ' models/'. $path. $model. php ')) {continue;} if ($db _conn!== FALSE and! class_exists (' ci_db ')) {if ($db _conn = = TRUE) {$db _conn = ';} $CI->load->database ($db _conn, FALSE, TRUE);} if (! class_exists (' Ci_model ')) {load_class (' Model ', ' core ');} require_once ($mod _path ' models/'. $path. $model. php '); $model = Ucfirst ($model);//The Model object is still registered as a member variable of the Controller class. Loader will do the same when loading other resources $ci-> $name = new $mOdel (); $this->_ci_models[] = $name; return;} Couldn ' t find the modelshow_error (' Unable to locate the model you had specified: '. $model);} *basepath/system/core/model.php//__get () is a magic method that is called when the value of an undefined variable is read//the following is an implementation of the model base class for the __get () function, so that within the model class, You can read its variable function __get ($key) {$CI =& get_instance () as if it were directly within the Controller class (for example, $this->var), return $CI $key;}
http://www.bkjia.com/PHPjc/976036.html www.bkjia.com true http://www.bkjia.com/PHPjc/976036.html techarticle PHP CodeIgniter Framework Working principle research, CodeIgniter framework CodeIgniter (hereinafter referred to as CI, official website and China Station) is a popular PHP framework, small but powerful, simple and lightweight ...