Study on the working principle of PHPCodeIgniter framework

Source: Internet
Author: User
Tags file separator
The working principle of PHPCodeIgniter framework. PHPCodeIgniter Framework Working principle research this article mainly introduces the PHPCodeIgniter Framework Working principle research, this article first analyzes its workflow, then, it summarizes the work of the former PHP CodeIgniter framework.

This article mainly introduces the working principle of PHP CodeIgniter framework. This article first analyzes its workflow and summarizes its working principles. For more information, see

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

// * BASEPATH/system/core/Common. php

// Benchmark, Hooks, and Config in the boot file are all loaded using this function.

Function & load_class ($ class, $ directory = 'libraries', $ prefix = 'ci _')

{

// Record the loaded class

Static $ _ classes = array ();

// Has been loaded, read directly and return

If (isset ($ _ classes [$ class])

{

Return $ _ classes [$ class];

}

$ Name = FALSE;

// Find the class to be loaded 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;

}

}

// Not found

If ($ name = FALSE)

{

Exit ('unable to locate the specified class: '. $ class.'. php ');

}

// Trace the loaded class. the is_loaded () function is shown below

Is_loaded ($ class );

$ _ Classes [$ class] = new $ name ();

Return $ _ classes [$ class];

}

// Record the loaded classes. The function returns all loaded classes.

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;

// Initialize all class objects in the boot file (CodeIgniter. php) (steps 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 the Loader object to load 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 a single instance of the controller.

Public static function & get_instance ()

{

Return self: $ instance;

}

}

// * BASEPATH/system/core/CodeIgniter. php

// Load the base controller class

Require BASEPATH. 'core/Controller. php ';

// Obtain the controller instance through this global function and obtain this super object,

// This function is called elsewhere in the program to gain control of the entire framework.

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

//.....

// Call the requested function

// The segment except class/function in uri 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. Only part of the code is listed here.

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 );

// Match the path of the model class in sequence. if the path is 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 );

// Register the model object as a member variable of the controller class. This is also true when Loader loads other resources.

$ CI-> $ name = new $ model ();

$ This-> _ ci_models [] = $ name;

Return;

}

// Couldn't find the model

Show_error ('unable to locate the model you have specified: '. $ model );

}

// * BASEPATH/system/core/Model. php

// _ Get () is a magic method. it is called to read the value of an undefined variable.

// The following is an implementation of the _ get () function of the Model base class, you can read its variables just like directly in the controller class (for example, $ this-> var ).

Function _ get ($ key)

{

$ CI = & get_instance ();

Return $ CI-> $ key;

Http://www.bkjia.com/PHPjc/976533.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/976533.htmlTechArticlePHP CodeIgniter framework working principle research this article mainly introduces the PHP CodeIgniter framework working principle research, this article first analyzes its work flow, and then summarizes its work original...

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.