Working principle of PHP CodeIgniter framework, codeigniter framework
CodeIgniter (CI, official website and Chinese site) is a popular PHP framework. It is small but powerful, simple and lightweight, and has good scalability. It is also popular in China. On the other hand, CI has not kept pace with the times and does not support some features after PHP5.3, making it more suitable for older projects. Even so, CI is still an excellent framework, and it has a small kernel, elegant source code, and is suitable for learning.
CI is easy to use and can easily develop web applications. Let's take a look at the CI workflow (here the content is referenced from the http://codeigniter.org.cn/user_guide/overview/appflow.html)
,
1. index. php acts as the front-end controller and initializes the basic resources required to run CodeIgniter.
2. the Router checks the HTTP request to determine who will process the request.
3. If a Cache file exists, it bypasses the normal system execution sequence and is directly sent to the browser.
4. Security ). Before the Application Controller is loaded, HTTP requests and data submitted by any user are filtered.
5. The Controller loads models, core libraries, auxiliary functions, and other resources required to process specific requests.
6. The final View is rendered to the content in the Web browser. If Caching is enabled, the view is first cached and can be used for future requests.
The above provides a general process. How does the program work internally when the page is displayed in the browser?
The following lists the files loaded by the CI framework in sequence and briefly introduces their functions:
01. index. php
Define the ENVIRONMENT (ENVIRONMENT), Framework path (system_path, BASEPATH), application directory (application_folder), Application Path (APPPATH), and load (require) CI core files
02. BASEPATH/core/CodeIgniter. php(Ps. Actually, BASEPATH contains the final file separator '/'. Here '/' is added for clearer display)
The system initialization file, the core part of the entire framework, loads a series of base classes and executes this request.
03. BASEPATH/core/Common. php
The common file contains a series of basic and public functions for global use, such as load_class () and get_config ().
04. BASEPATH/core/Benchmark
This is a benchmark test class. By default, the execution points of each stage of the application are marked to get the execution time. You can also define monitoring points by yourself.
05. BASEPATH/core/Hooks. php
CI_Hooks is a hook class, which is the core of framework extension. It can insert hook points at various stages allowed by the program and execute your custom classes and functions.
06. BASEPATH/core/Config. php
Configuration File Management class, loading and reading or setting Configuration
07. BASEPATH/core/URI. php, BASEPATH/core/Router. php
The URI class helps you parse the request uri and provides a set of functions to split the uri for the Router class to use.
08. BASEPATH/core/Router. php
Routing class, that is, through the request uri, and the route configured by the user (APPPATH/config/routes. php) to distribute user requests to the specified processing function (usually an action function in a Controller instance)
09. BASEPATH/core/Output. php, BASEPATH/core/Input. php
The input class is used to process the input parameters of a request and provides a safe way to obtain the input parameters. The output class sends the final execution result. It is also responsible for caching.
10. BASEPATH/core/Controller. php
The controller base class provides external instances in singleton mode, and the heart of the entire application. It is a Super Object, and classes loaded in the application can all be controller member variables. This is very important and will be discussed later.
11. APPPATH/controllers/$ RTR-> fetch_directory (). $ RTR-> fetch_class (). '. php'
Obtain the Controller name through the routing function and instantiate the real controller class (subclass)
12. 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 as a member variable of the controller.
13. call_user_func_array calls the Handler
Obtain the action function name through routing. Call the Controller-> action () function to process the application logic. The actual business processing logic is written in the action function.
14. $ OUT-> _ display () outputs the content
The above is the most basic processing process of the entire application. The following describes the core content code to enhance the CI understanding:
<? Php // * BASEPATH/system/core/Common. php // Benchmark, Hooks, and Config in the boot file are all functions loaded through this function & load_class ($ class, $ directory = 'libraries ', $ prefix = 'Ci _ ') {// record the loaded class static $ _ classes = array (); // already loaded, directly read and return if (isset ($ _ classes [$ class]) {return $ _ classes [$ class] ;}$ name = FALSE; // find the class foreach (array (APPPATH, BASEPATH) as $ path) to be loaded in the specified directory {if (file_exists ($ path. $ directory. '/'. $ class. '. php ') {$ name = $ prefix. $ Class; if (class_exists ($ name) === FALSE) {require ($ path. $ directory. '/'. $ class. '. php ');} break;} // if ($ name = FALSE) {exit ('unable to locate the specified class :'. $ class. '. php ');} // trace the loaded class. The is_loaded () function is shown in is_loaded ($ class ); $ _ classes [$ class] = new $ name (); return $ _ classes [$ class] ;}// record the loaded classes. The function returns all loaded class Functions & 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; public function _ construct () {self: $ instance = & $ this; // All in the pilot file (CodeIgniter. php) initialize the Class object (such as step 4, 5, 6, 7, 8, 9), // register as a member variable of the controller class, so that the controller becomes a super object) foreach (is_loaded () as $ var => $ class) {$ this-> $ var = & load_class ($ class) ;}< span style = "white -Space: pre "> </span> // load the Loader object and use the Loader object to load a series of resources in 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 public static function & get_instance () {return self :: $ instance ;}// * BASEPATH/system/core/CodeIgniter. php // Load the base controller classrequire BASEPATH. 'Core/Contr Oller. php '; // The Controller instance is obtained through this global function and the super object is obtained. // This function is called elsewhere in the program, you can get the control function & get_instance () {return CI_Controller: get_instance () ;}// load the corresponding controller class // note: the Router class automatically uses 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. 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 sections except class/function in the function // uri that calls the request will also be passed to the called 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 // Let's look at an example of loading a model for the Loader class. The public function model ($ model, $ name = '', $ db_conn = FALSE) {$ CI = & get_instance (); if (isset ($ CI-> $ name) {show_error ('the model name you are loading is The name of a resource that is already being used :'. $ name) ;}$ model = strtolower ($ model); // matches the path of the model class in sequence, if yes, 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 does this 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 have specified :'. $ model);} // * BASEPATH/system/core/Model. php // _ get () is a magic method. when reading the value of an undefined variable, it will be called // The following is the Model base class pair _ get () an Implementation of the function allows the Model class to be directly in the Controller class (for example, $ this-> var) read its variable function _ get ($ key) {$ CI = & get_instance (); return $ CI-> $ key ;}