CI Framework Source Reading Note 8 Controller controller.php
The recent time is a little tight, source reading series update some slow. Since the controller in the code is relatively small, this blog first update the source of the file analysis. After a routed distribution, the actual application controller takes over all the user's requests and is responsible for interacting with the user's data. All application controllers in CI should be subclasses of Ci_controller (unless you extend the core of CI, your controller parent class can be My_controller). In the application controller, we often use this code:/* Load config file */$this->load->config ("Config_app"); /* Load Model */$this->load->model ("user"); /* Load View */$this->load->view ("index"); /* Get post */$this->input->post ("Data", true); /* Get Get */$this->input->get ("Data", true); /* Clear XSS */$this->security->xss_clean ($data); /* Mark Time-point */$this->benchmark->mark ("App_start"); How are these implemented? We'll follow up briefly. Although the structure of this class is very simple, we still post the Ci_controller class diagram: 1. _contruct () constructor here CI does a processing, adding all the loaded components to Ci_controller (as we've seen before, the is_loaded function tracks all loaded components): foreach (is_loaded () as $var = > $class) {$this, $var =& load_class ($class);} Look at the components that is_loaded traces when the controller is instantiated: This explains why we can invoke the components of CI by $this->input and so on. This is not enough, by the way Loader also get in: $this->load =& load_class (' Loader ', ' core '); $this->load->initialize (); Now you can use the loader component to load a($this->load->config), load the model ($this->load->model) and load the view ($this->load->view) ci_ A controller can be said to be a super class that holds multiple components, which is very similar to the "proxy mode" in design mode. 2. &get_instance Here's a simple explanation, Ci_controller is a singleton pattern class that obtains an instance of the class through the Get_instance () method. This method is called by the Get_instance function in codeigniter.php: public static function &get_instance () {return self:: $instance;} Here are some hint:1 about the controller. You can customize the directory in the controller in the CI, such as creating the directory admin in the Application/controller directory, and create a new Indexcontroller, The controller's URL access path is: TEST.XQ.COM/ADMIN/INDEX/2. The controller should not assume too much logic and the business logic should be encapsulated in the model. 3. Your controller should be differentiated by business, such as usercontroller processing user-related requests, and AppController processing application requests, and so on, which is not a principle, but only a way. 4. The Controller class name should start with an uppercase letter and the file name should be in the form of all lowercase. 5. The method that begins with an underscore in the controller is considered by CI to be a private method and cannot be directly accessed externally. The above is the controller's entire content. Finally, the source of Ci_controller is posted: Class Ci_controller {private static $instance;/** * Constructor */Public function __construct ( {self:: $instance =& $this; 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 '); public static function &get_instance () {return self:: $instance;}
http://www.bkjia.com/PHPjc/914774.html www.bkjia.com true http://www.bkjia.com/PHPjc/914774.html techarticle CI Framework Source Reading notes 8 controller controller.php The recent time is somewhat tight, source reading series update some slow. Since the controller in the code is relatively small, this blog first update the source of the file ...