CI Framework Source Reading Note 8 Controller controller.php,cicontroller.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 */$this->benchmark->mark (" App_ Start ");
How are these implemented? Let's just follow up a little bit.
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 the loaded components):
foreach (is_loaded () as $var = = $class) { $this, $var =& load_class ($class);}
Look at the components that is_loaded tracks 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 ();
You can now use the loader component to load the configuration ($this->load->config), load the model ($this->load->model), and load the view ($ This->load->view)
Ci_controller can be said to be a super class that holds multiple components in such a way that it 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 gets 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 about the controller:
1. In the controller in CI can customize the directory, for example, in the Application/controller directory to create the directory admin, and new Indexcontroller, the controller's URL access path is:
test.xq.com/admin/index/
2. The Controller should not assume excessive 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, it is to post the source code of Ci_controller:
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; }}
References in this article:
http://www.bkjia.com/PHPjc/914485.html www.bkjia.com true http://www.bkjia.com/PHPjc/914485.html techarticle CI Framework Source Reading notes 8 controller controller.php,cicontroller.php The recent time is somewhat tight, source reading series update some slow. In view of the controller in the code is relatively small, this blog first more ...