CI framework source code reading: ControllerController.php

WBOY
Release: 2016-08-08 09:33:20
Original
1406 people have browsed it

CI framework source code reading: controllerController.php

 Time is a little tight recently, and the source code reading series updates are a little slow. 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:

/* 加载配置文件 */
$this->load->config("config_app");

/* 加载model */
$this->load->model("user");

/* 加载视图 */
$this->load->view("index");

/* 获取post */
$this->input->post("data",true);

/* 获取 get */
$this->input->get("data",true);

/* 清除xss */
$this->security->xss_clean($data);

/* mark时间点 */
$this->benchmark->mark("app_start");
Copy after login

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);
}
Copy after login

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();
Copy after login

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

Here is a brief explanation, 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;
}
Copy after login

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/
Copy after login

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, etc. 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;
    }
}
Copy after login


The above introduces the CI framework source code reading: Controller.php, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!