


CI framework source code reading notes 8 Controller.php, cicontroller.php_PHP tutorial
Jul 13, 2016 am 10:13 AMCI 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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
References for this article:

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

Golang framework documentation best practices

How to choose the best golang framework for different application scenarios

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

Performance comparison of Java frameworks

Detailed practical explanation of golang framework development: Questions and Answers

Golang framework performance comparison: metrics for making wise choices
