The working principle of PHP CodeIgniter Framework _php Tutorial

Source: Internet
Author: User
Tags benchmark php framework codeigniter

Research on the working principle of PHP CodeIgniter framework


This article mainly introduces the working principle of PHP CodeIgniter framework, this article first analyzes its work flow, and then summarizes its working principle, the need for friends can refer to the next

CodeIgniter (hereinafter referred to as CI, official website and China Station) is a popular PHP framework, small but powerful, simple and lightweight and has a good extensibility, is also more popular in the country. CI, on the other hand, does not advance with the times and does not support some of the features behind PHP5.3, which makes it relatively more suitable for older projects. Nevertheless, CI is still an excellent framework, and it has a small kernel, elegant source code, suitable for learning.

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



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

The above gives a general flow. So how does it work inside the program when it sees the page in the browser?
The following sequence of executions lists the main files loaded by the CI framework, with a brief description of their role:

index.php.
Define usage environment (environment), Frame Path (system_path,basepath), application directory (application_folder), Application path (APPPATH), etc., load (require) CI core file
Basepath/core/codeigniter.php (Ps. Actually basepath contains the final file delimiter '/', where additional '/' is for clearer presentation)
The system initialization file, the core part of the entire framework, loads a series of base classes here, and executes the request
basepath/core/common.php.
The common file contains a series of basic and public functions for global use, such as Load_class (), get_config (), etc.
Basepath/core/benchmark.
This is a benchmark class that, by default, marks the execution point of each stage 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 that is the core of the framework to be expanded to insert hook points at various stages of the program, execute your custom classes, functions, etc.
basepath/core/config.php.
Profile Management class, load read or set configuration
basepath/core/uri.php, basepath/core/router.php
The URI class helps you parse the URI of the request 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 user requests to a specified handler (typically an action function in a controller instance) by requesting a URI, and a user-configured route (apppath/config/routes.php)
basepath/core/output.php, basepath/core/input.php
The input class, which is the input parameter that processes the request, provides a secure way to get it. The output class sends the final execution result, and it is responsible for caching the function
Ten. basepath/core/controller.php
Controller base class, using singleton mode to provide an example to the entire application of the heart. It is a super Object, and it is important that classes loaded within an application can become member variables of the controller, which you will continue to talk about 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, etc. in the application and set the member variables to become controllers
Call_user_func_array calling a handler function
By routing, the action function name is obtained, the controller->action () function is called, the application logic is processed, and the actual business processing logic is written in the action function.
$OUT->_display () to output the content

These are the most basic processing processes for the entire application. The following selection of core content code to explain, to enhance the understanding of CI:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

The benchmark,hooks,config in the boot file is loaded by 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 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

}

}

Not found

if ($name = = = FALSE)

{

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

}

Trace the class that was just loaded, the is_loaded () function below

Is_loaded ($class);

$_classes[$class] = new $name ();

return $_classes[$class];

}

Record classes that have already been loaded. 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;

Codeigniter.php all class objects initialized in the boot file (that is, the steps just 4,5,6,7,8,9),

Registering as a member variable of the Controller class makes the controller a Super object

foreach (is_loaded () as $var = = $class)

{

$this $var =& Load_class ($class);

}

//Load the loader object, and then use the loader object to load a series of resources within 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 externally

public static function &get_instance ()

{

Return self:: $instance;

}

}

*basepath/system/core/codeigniter.php

Load the base Controller class

Require basepath. ' core/controller.php ';

With this global function, we get the example of the controller and get the Super object,

means to call this function 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 will automatically use 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 do 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

//.....

Calling the requested function

Segments other than class/function in the URI are also passed to the calling 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

See an example of a loader class loading model. 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 loading is the ' The name of the ' a ' resource, ' is already being used: '. $name);

}

$model = Strtolower ($model);

Match the path of the model class in turn, and if found, loads the

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 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, the model you have specified: '. $model);

}

*basepath/system/core/model.php

__get () is a magic method that is called when a value of an undefined variable is read

The following is an implementation of the model base class for the __get () function, so that in the model class, you can read its variables as if it were directly inside the controller class (for example, $this->var)

function __get ($key)

{

$CI =& get_instance ();

return $CI $key;

http://www.bkjia.com/PHPjc/976533.html www.bkjia.com true http://www.bkjia.com/PHPjc/976533.html techarticle A study on the working principle of PHP CodeIgniter framework This article mainly introduces the work principle of PHP CodeIgniter framework, 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.