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 php5.2 and previous projects. Even so, CI is still an excellent framework with a small kernel and elegant source code, which is suitable for learning.
CI is easy to use and can easily develop web applications. Let's first review the CI workflow (here the content is referenced from the http://codeigniter.org.cn/user_guide/overview/appflow.html)
- Index. php acts as the front-end controller and initializes the basic resources required to run codeigniter.
- The router checks the HTTP request to determine who will process the request.
- If a cached file exists, it bypasses the normal system execution sequence and is directly sent to the browser.
- Security ). Before the application controller is loaded, HTTP requests and data submitted by any user are filtered.
- The Controller loads models, core libraries, auxiliary functions, and other resources required to process specific requests.
- The final view rendering is sent 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 gives a general concept. Let's take a look at how the program works when we see the page displayed in a 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 the 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 processing function
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 (): Output content
The above is the most basic processing process of the entire application. The following describes the core content code:
<? 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];} // records the loaded classes, and return all 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 (Step 4, 5, 6, 7, 8, 9), // assign a member variable to the Controller class, so that the controller becomes a super object) foreach (is_loaded () as $ var =>$ class) {$ this-> $ Var = & load_class ($ class) ;}$ 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 for a single instance of the controller. PHP // load the base controller classrequire basepath. 'Core/controller. PHP '; // The Controller instance is obtained through this global function and the super object is obtained. // This function is called elsewhere, you can obtain the control function & get_instance () {return ci_contro of the entire framework. Roller: 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. Note that the public function model ($ model, $ name = '', $ db_conn = false) {$ CI = & get_instance () is not provided here (); 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); // here, the model object is still assigned as a member variable of the controller class $ 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 an implementation of the model class, so that the model class can read its variable function _ Get ($ key) Just like directly in the Controller class (for example, $ this-> var) {$ CI = & get_instance (); return $ ci-> $ key ;}
--- To be continued.
Analysis on the working principle of codeigniter framework