search
HomeBackend DevelopmentPHP TutorialCI framework source code reading notes 8 Controller.php_PHP tutorial

CI Framework Source Code Reading Notes 8 Controller.php

Time has been tight recently, and the source code reading series has been a bit slow to update. 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:
/* Load 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 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? 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);
}
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();
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
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:
public static function &get_instance()
{
return self::$instance;
}
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/
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 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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914774.htmlTechArticleCI Framework Source Code Reading Notes 8 Controller Controller.php Time has been tight recently, and the source code reading series updates are a bit slow. In view of the relatively small code in Controller, this blog will update the source of the file first...
Statement
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
Windows 11 上正确校准 Xbox One 控制器的方法Windows 11 上正确校准 Xbox One 控制器的方法Sep 21, 2023 pm 09:09 PM

由于Windows已成为首选的游戏平台,因此确定其面向游戏的功能就显得尤为重要。其中之一是能够在Windows11上校准XboxOne控制器。借助内置的手动校准,您可以摆脱漂移、随机移动或性能问题,并有效地对齐X、Y和Z轴。如果可用选项不起作用,您可以随时使用第三方XboxOne控制器校准工具。让我们来了解一下!如何在Windows11上校准我的Xbox控制器?在继续操作之前,请确保将控制器连接到电脑并更新XboxOne控制器的驱动程序。当您使用它时,还要安装任何可用的固件更新。1.使用Wind

从零开始学习Laravel:控制器方法调用详解从零开始学习Laravel:控制器方法调用详解Mar 10, 2024 pm 05:03 PM

从零开始学习Laravel:控制器方法调用详解在Laravel的开发中,控制器是一个非常重要的概念。控制器起到了连接模型和视图的桥梁作用,负责处理来自路由的请求,并返回相应的数据给视图展示。控制器中的方法可以被路由调用,这篇文章将详细介绍如何编写并调用控制器中的方法,同时会提供具体的代码示例。首先,我们需要创建一个控制器。可以使用Artisan命令行工具来生

Python在软件源码保护中的应用实践Python在软件源码保护中的应用实践Jun 29, 2023 am 11:20 AM

Python语言作为一种高级编程语言,具有简单易学、易读易写等特点,在软件开发领域中得到了广泛的应用。然而,由于Python的开源特性,源代码很容易被他人轻易获取,这就给软件源码保护带来了一些挑战。因此,在实际应用中,我们常常需要采取一些方法来保护Python源代码,确保其安全性。在软件源码保护中,有多种针对Python的应用实践可供选择。下面将介绍几种常见

php如何使用CodeIgniter4框架?php如何使用CodeIgniter4框架?May 31, 2023 pm 02:51 PM

PHP是一种非常流行的编程语言,而CodeIgniter4是一种常用的PHP框架。在开发Web应用程序时,使用框架是非常有帮助的,它可以加速开发过程、提高代码质量、降低维护成本。本文将介绍如何使用CodeIgniter4框架。安装CodeIgniter4框架CodeIgniter4框架可以从官方网站(https://codeigniter.com/)下载。下

什么叫laravel控制器什么叫laravel控制器Jan 14, 2023 am 11:16 AM

在laravel中,控制器(Controller)是一个类,用于实现一定的功能;控制器能将相关的请求处理逻辑组成一个单独的类。控制器中存放中一些方法,实现一定的功能,通过路由调用控制器,不再使用回调函数;控制器被存放在“app/Http/Controllers”目录中。

Laravel学习指南:控制器方法调用的最佳实践Laravel学习指南:控制器方法调用的最佳实践Mar 11, 2024 am 08:27 AM

在Laravel学习指南中,控制器方法的调用是一个非常重要的主题。控制器扮演着连接路由和模型的桥梁的角色,在应用程序中起着至关重要的作用。本文将介绍控制器方法调用的最佳实践,并提供具体的代码示例帮助读者更好地理解。首先,让我们来了解控制器方法的基本结构。在Laravel中,控制器类通常存放在app/Http/Controllers目录下,每个控制器类包含多个

PHP代码在浏览器中如何显示源码而不被解释执行?PHP代码在浏览器中如何显示源码而不被解释执行?Mar 11, 2024 am 10:54 AM

PHP代码在浏览器中如何显示源码而不被解释执行?PHP是一种服务器端脚本语言,通常用于开发动态网页。当PHP文件在服务器上被请求时,服务器会解释执行其中的PHP代码,并将最终的HTML内容发送到浏览器以供显示。然而,有时我们希望在浏览器中直接展示PHP文件的源代码,而不是被执行。本文将介绍如何在浏览器中显示PHP代码的源码,而不被解释执行。在PHP中,可以使

网站在线看源码网站在线看源码Jan 10, 2024 pm 03:31 PM

可以使用浏览器的开发者工具来查看网站的源代码,在Google Chrome浏览器中:1、打开 Chrome 浏览器,访问要查看源代码的网站;2、右键单击网页上的任何位置,然后选择“检查”或按下快捷键 Ctrl + Shift + I打开开发者工具;3、在开发者工具的顶部菜单栏中,选择“Elements”选项卡;4、看到网站的 HTML 和 CSS 代码即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version