CI Framework Source Reading Note 8 Controller controller.php

Source: Internet
Author: User
Tags uppercase letter codeigniter

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:
    1. http://blog.163.com/wu_guoqing/blog/static/196537018201281663649361/
    2. Http://codeigniter.org.cn/user_guide/database/helpers.html

CI Framework Source Reading Note 8 Controller controller.php

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.