Table of Contents
CI framework source code reading notes 8 Controller.php, cicontroller.php
1. _construct() constructor
2. &get_instance
References for this article:
Home Backend Development PHP Tutorial CI framework source code reading notes 8 Controller.php, cicontroller.php_PHP tutorial

CI framework source code reading notes 8 Controller.php, cicontroller.php_PHP tutorial

Jul 13, 2016 am 10:13 AM
controller frame Source code notes read

CI framework source code reading notes 8 Controller.php, cicontroller.php

Time has been tight recently, and the source code reading series updates are a bit 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 the user's requests 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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/* 加载配置文件 */

$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 earlier that the is_loaded function tracks all loaded components):

1

2

3

4

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:

1

2

3

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

 

$this->load->initialize();

Copy after login

Now, you can use the Loader component to load the configuration ($this->load->config) and 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 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:

1

2

3

4

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:

1

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. 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 all the content of Controller.

Finally, the source code of CI_Controller is posted:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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

References for this article:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914485.htmlTechArticleCI framework source code reading notes 8 ControllerController.php, cicontroller.php Time is a little tight recently, source code reading series updates Somewhat slow. In view of the relatively small code in Controller, this blog will update first...
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to evaluate the cost-effectiveness of commercial support for Java frameworks How to evaluate the cost-effectiveness of commercial support for Java frameworks Jun 05, 2024 pm 05:25 PM

How to evaluate the cost-effectiveness of commercial support for Java frameworks

How do the lightweight options of PHP frameworks affect application performance? How do the lightweight options of PHP frameworks affect application performance? Jun 06, 2024 am 10:53 AM

How do the lightweight options of PHP frameworks affect application performance?

Golang framework documentation best practices Golang framework documentation best practices Jun 04, 2024 pm 05:00 PM

Golang framework documentation best practices

How to choose the best golang framework for different application scenarios How to choose the best golang framework for different application scenarios Jun 05, 2024 pm 04:05 PM

How to choose the best golang framework for different application scenarios

How does the learning curve of PHP frameworks compare to other language frameworks? How does the learning curve of PHP frameworks compare to other language frameworks? Jun 06, 2024 pm 12:41 PM

How does the learning curve of PHP frameworks compare to other language frameworks?

Performance comparison of Java frameworks Performance comparison of Java frameworks Jun 04, 2024 pm 03:56 PM

Performance comparison of Java frameworks

Detailed practical explanation of golang framework development: Questions and Answers Detailed practical explanation of golang framework development: Questions and Answers Jun 06, 2024 am 10:57 AM

Detailed practical explanation of golang framework development: Questions and Answers

Golang framework performance comparison: metrics for making wise choices Golang framework performance comparison: metrics for making wise choices Jun 05, 2024 pm 10:02 PM

Golang framework performance comparison: metrics for making wise choices

See all articles