The working principle of PHP CodeIgniter framework _php Skills

Source: Internet
Author: User
Tags benchmark php class php framework codeigniter

CodeIgniter (CI, official website and China Station) is a popular PHP framework, compact but powerful, simple lightweight at the same time have a good scalability, at home is also relatively popular. CI, on the other hand, has not been with the times, and it does not support some of the features behind PHP5.3, resulting in its relative suitability for older projects. Even so, CI is still an excellent framework, and its own core is small, the source code elegant, suitable for learning.

CI is easy to use and can be easily developed for Web applications. Let's take a look at the work flow Chart of CI (the content is quoted from http://codeigniter.org.cn/user_guide/overview/appflow.html)



1.index.php as a front-end controller, initialize the basic resources needed to run CodeIgniter.
2.Router Check the HTTP request to determine who will handle the request.
3. If the cache file exists, it bypasses the usual system execution order and is sent directly to the browser.
4. Safety (security). The HTTP request and any user-submitted data will be filtered before the application controller (application Controller) is loaded.
5. The controller (Controller) loads the model, core library, auxiliary functions, and any other resources required to process a particular request.
6. Final view rendering is sent to the content in the Web browser. If you turn on caching (Caching), the view is cached first, so it will be available for future requests.

The above gives a rough flow. So when you see the page in the browser, how does the inside of the program work?
Following the order of execution, the main loaded files for the CI framework are listed in turn, and their role is briefly described:

index.php.
Define the usage environment (environment), Framework Path (SYSTEM_PATH,BASEPATH), application directory (application_folder), Application path (AppPath), etc., load (require) CI core files
basepath/core/codeigniter.php(Ps. Actually basepath contains the last file delimiter '/', which is added '/' for a clearer display)
System initialization file, the most central part of the entire framework, loading (load) a series of base class, and executing this request
basepath/core/common.php
The common file contains a series of basic and common functions for global use, such as Load_class (), get_config (), etc.
Basepath/core/benchmark
This is a benchmark class that defaults to the execution point of each phase 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, which is the core of the framework to extend, can be inserted at various stages of the program allows the hook point, the implementation of your custom classes, functions, etc.
basepath/core/config.php
Configuration file Management class, loading read or set configuration
basepath/core/uri.php, basepath/core/router.php
The URI class helps you parse the requested URI 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 a user request to a specified handler (usually an action function in a controller instance) through the requested URI, and the user-configured route (apppath/config/routes.php)
basepath/core/output.php, basepath/core/input.php
The input class, which is the input parameter that handles the request, provides a secure way to obtain it. The output class sends out the final execution result, and it is also responsible for caching functionality
basepath/core/controller.php
Controller base class, with a singleton mode to provide an instance of the entire application to the heart. It is a super Object, and it is important that classes loaded within the application can become member variables of the controller, which will continue to be mentioned 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, and so on in the application and set it as a member variable for the controller
Call_user_func_array Call processing function
By routing, you get the name of the action function, call the Controller->action () function, process the application logic, and the actual business process logic is written in the action function.
$OUT->_display () outputs the content

These are the most basic processing processes for the entire application. Below, select the core content code and then explain it to enhance the understanding of CI:

<?php//*basepath/system/core/common.php//boot files, benchmark,hooks,config, and so on, are functions that are loaded through this function &load_class ($

		class, $directory = ' libraries ', $prefix = ' ci_ ') {//Record loaded classes static $_classes = Array ();
		has already been loaded, directly read and return if (Isset ($_classes[$class)) {returns $_classes[$class];

		} $name = FALSE; Looks for classes to load in the specified directory 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; The IF ($name = = FALSE) {exit (' Unable to locate specified class: '. $class. ') is not found.
		php ');

		///Track records just Loaded class, is_loaded () function below is_loaded ($class);
		$_classes[$class] = new $name ();
	return $_classes[$class]; //Log The class that has already been loaded.

		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.php class Ci_controller {private static $instance;
		
		Public Function __construct () {self:: $instance =& $this; All Class objects initialized (codeigniter.php) in the boot file (that is, steps just 4,5,6,7,8,9),//registered as member variables of the controller class, make this controller a Super object (Super Objects) foreach (IS
		_loaded () as $var => $class) {$this-> $var =& load_class ($class); <span style= "White-space:pre" > </span>//loads Loader objects, and then uses loader objects to load a series of resources within the program <span style= "

		White-space:pre "> </span> $this->load =& load_class (' Loader ', ' core ');
		
		$this->load->initialize ();
	Log_message (' Debug ', "Controller Class initialized");
	///This function provides a single instance of the controller publicly static function &get_instance () {return self:: $instance;

	}//*basepath/system/core/codeigniter.php//Load the base controller class require BasePath. ' core/controller.php ';
This global function gives an example of the controller and gets the Super object,//means that the function is invoked elsewhere in the program to get control of the entire frame function &get_instance ()	{return ci_controller::get_instance (); ///load the corresponding Controller class//Note: The Router class automatically validates the controller path with Router->_validate_request () (! file_exists apppath. ' controllers/'. $RT R->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 '); $class = $RTR->fetch_class (); Controller class name $method = $RTR->fetch_method (); Action name//... the function//URI in which the request is invoked is passed to the called Function Call_user_func_array (& $CI, in addition to Class/function) ($method

	), Array_slice ($URI->rsegments, 2));
	Outputs the final content to the browser if ($EXT->_call_hook (' display_override ') = FALSE) {$OUT->_display (); //*basepath/system/core/loader.php//See a Loader class load model example.
		Only some of the code public Function model ($model, $name = ', $db _conn = FALSE) are listed here {$CI =& get_instance (); if (Isset ($CI-> $naMe) {show_error (' The model name you are loading is the name of a resource this is already being used: '. $name);

		} $model = Strtolower ($model); In turn, according to the model class path to match, if found on the load foreach ($this->_ci_model_paths as $mod _path) {if (! file_exists $mod _path. ' MoD els/'. $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 Model Show_error ("Unable to locate" model you have: '. $model); //*basepath/system/core/model.php//__get () is a magic method that is invoked when reading the value of an undefined variableThe 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 =&, as directly within the Controller class (such as $this->var) get_
		Instance ();
	return $CI-> $key; }

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.