CI Framework Source Code Reading Notes 8 Controller.php
Time has been tight recently, and the source code reading series has been a bit slow to update. Since there is relatively little code in Controller, this blog will first update the source code analysis of this file.
After routing and distribution, the actual application Controller takes over all requests from the user and is responsible for interacting with user data. All application controllers in CI should be subclasses of CI_Controller (unless you extend the core of CI, then your Controller parent class can be MY_Controller).
In application controllers, we often use code like this:
/* Load configuration 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? Let’s simply follow them next.
Although the structure of this class is very simple, we still post the class diagram of CI_Controller:
1. _construct() constructor
Here CI does a process and adds all loaded components to CI_Controller (we have seen before that the is_loaded function tracks all loaded components):
foreach (is_loaded() as $var => $class)
{
$this->$var =& load_class($class);
}
Look at the components tracked by is_loaded when the Controller is instantiated:
This explains why we can call CI components through $this->input and other methods.
This is not enough, let’s also bring in the Loader:
$this->load =& load_class('Loader', 'core');
$this->load->initialize();
Now, you can use the Loader component to load configuration ($this->load->config), load model ($this->load->model) and load view ($this->load ->view)
CI_Controller can be said to be a super class that holds multiple components. In this way, it is very similar to the "agent pattern" in the design pattern.
2. &get_instance
A brief explanation here is that CI_Controller is a singleton mode class, and an instance of this class is obtained 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;
}
The following are some Hints about Controller:
1. The directory can be customized in the Controller in CI. For example, create the directory admin in the application/controller directory and create a new IndexController. Then the URL access path of the Controller is:
test.xq.com/admin/index/
2. The Controller should not bear too much logic, and the business logic should be encapsulated into the Model.
3. Your Controller should be differentiated according to business. For example, UserController handles user-related requests, while AppController handles application requests. This is not a principle, but just a way.
4. The Controller class name should start with a capital letter, and the file name should be all lowercase.
5. Methods starting with an underscore in the Controller are considered private methods by CI and cannot be directly accessed by the outside.
The above is the entire content of Controller.
Finally, the source code 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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914774.htmlTechArticleCI Framework Source Code Reading Notes 8 Controller Controller.php Time has been tight recently, and the source code reading series updates are a bit slow. In view of the relatively small code in Controller, this blog will update the source of the file first...