Study on the working principle of PHPCodeIgniter framework, codeigniter framework _ PHP Tutorial

Source: Internet
Author: User
Tags file separator
The working principle of PHPCodeIgniter framework is studied. Working Principle of PHPCodeIgniter framework: codeigniter framework CodeIgniter (hereinafter referred to as CI, official website and Chinese site) is a popular PHP framework, which is small but powerful, simple and lightweight PHP CodeIgniter framework working principle research, 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) ;}// load the Loader object and use Lo The ader object loads a series of resources in the program. $ 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/Controller. 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 control of the entire framework. Unction & get_instance () {return CI_Controller: get_instance () ;}// load the corresponding controller class // Note: The Router class automatically uses router-> _ validate_request () 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 ;}

Http://www.bkjia.com/PHPjc/976036.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/976036.htmlTechArticlePHP CodeIgniter framework working principle research, codeigniter framework CodeIgniter (hereinafter referred to as CI, official website and Chinese site) is a popular PHP framework, small but powerful, simple and lightweight...

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.