CI framework Source Code Reading Notes 8 Controller. php, cicontroller. php

Source: Internet
Author: User

CI framework Source Code Reading Notes 8 Controller. php, cicontroller. php

The recent time is a little tight, and the source code reading series updates are a little slow. In view of the few codes in the Controller, this Blog first updates the source code analysis of this file.

After route distribution, the actual application Controller takes over all users' requests and interacts with user data. All application controllers in CI should be sub-classes of CI_Controller (unless you have extended the core of CI, your Controller parent class can be MY_Controller ).

This code is often used in application controllers:

/* Load the 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 */$ this-> input-> get ("data", true ); /* clear xss */$ this-> security-> xss_clean ($ data);/* mark time point */$ this-> benchmark-> mark ("app_start ");

How are these implemented? Next, let's take a simple look.

Although the structure of this class is very simple, we still paste the class diagram of CI_Controller:

 

1. _ contruct () constructor

Here CI makes a process to add all loaded components to CI_Controller (as we have seen before, the is_loaded function traces all loaded components ):

foreach (is_loaded() as $var => $class){    $this->$var=& load_class($class);}

Let's see what components is_loaded tracks when the Controller is instantiated:

 

This explains why we can use $ this-> input to call CI components.

This is not enough. By the way, the Loader is also involved:

$this->load =& load_class('Loader','core'); $this->load->initialize();

Now, you can use the Loader component to load configurations ($ this-> load-> config) and models ($ this-> load-> model) and load the view ($ this-> load-> view)

CI_Controller is a super class that holds multiple components. This method is very similar to the "proxy mode" in the design mode ".

2. & get_instance

Here, we will briefly explain that CI_Controller is a class in singleton mode. The instance of this class is obtained through the get_instance () method. In CodeIgniter. php, The get_instance function calls this method:

public static function &get_instance(){     returnself::$instance;}

The following are some hints about the Controller:

1. The Controller in CI can customize directories. For example, if you create a directory admin in the application/controller directory and create an IndexController, the URL access path of the Controller is:

test.xq.com/admin/index/

2. The Controller should not carry too much logic, and the business logic should be encapsulated in the Model.

3. Your Controller should be differentiated by business. For example, UserController processes user-related requests, while AppController processes application requests. This is not a principle, but a method.

4. The Controller class name should start with an uppercase letter, and the file name should be in full lowercase format.

5. The method starting with an underscore in the Controller is considered as a private method by CI and cannot be directly accessed by the outside.

The above is all about the Controller.

Finally, the source code of CI_Controller is posted:

class CI_Controller {     privatestatic $instance;     /**     * Constructor     */    publicfunction __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");    }     publicstatic function&get_instance()    {        returnself::$instance;    }}
References in this article:

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.