Table of Contents
PHP CodeIgniter框架的工作原理研究,codeigniter框架
Home php教程 php手册 PHP CodeIgniter框架的工作原理研究,codeigniter框架

PHP CodeIgniter框架的工作原理研究,codeigniter框架

Jun 13, 2016 am 09:08 AM
codeigniter php working principle frame

PHP CodeIgniter框架的工作原理研究,codeigniter框架

CodeIgniter(以下简称CI,官网以及中国站)是一个流行的PHP框架,小巧但功能强大,简洁轻量同时拥有很好的扩展性,在国内也比较受欢迎。另一方面,CI却没有与时俱进,并不支持PHP5.3之后的一些特性,导致它相对更适合较老一些的项目。虽然如此,CI仍是一个优秀的框架,而且它本身内核较小,源码优雅,适于学习。

CI易于使用,可以方便的开发出web应用。先来看一下CI的工作流程图(此处内容引用自http://codeigniter.org.cn/user_guide/overview/appflow.html)



1.index.php 作为前端控制器,初始化运行 CodeIgniter 所需要的基本资源。
2.Router 检查 HTTP 请求,以确定谁来处理请求。
3.如果缓存(Cache)文件存在,它将绕过通常的系统执行顺序,被直接发送给浏览器。
4.安全(Security)。应用程序控制器(Application Controller)装载之前,HTTP 请求和任何用户提交的数据将被过滤。
5.控制器(Controller)装载模型、核心库、辅助函数,以及任何处理特定请求所需的其它资源。
6.最终视图(View)渲染发送到 Web 浏览器中的内容。如果开启缓存(Caching),视图首先被缓存,所以将可用于以后的请求。

以上给出了一个大致流程。那么当看到页面在浏览器中呈现时,程序内部究竟是如何工作的呢?
下面按照执行顺序,依次列出了CI框架主要加载的文件,并简要介绍其作用:

01. index.php
定义使用环境(ENVIRONMENT),框架路径(system_path,BASEPATH),应用目录(application_folder),应用路径(APPPATH)等,加载(require)CI核心文件
02. BASEPATH/core/CodeIgniter.php (ps.实际上BASEPATH包含最后的文件分隔符'/',这里额外加上了'/'是为了更清晰的展示)
系统初始化文件,整个框架最核心的部分,在此加载(load)了一系列的base class,并且执行这次请求
03. BASEPATH/core/Common.php
common文件包含一系列的基础和公共函数 ,供全局使用,例如load_class(),get_config()等
04. BASEPATH/core/Benchmark
这是一个基准测试类,默认标注了应用各个阶段的执行点,以得到其执行时间。也允许你自己定义监测点。
05. BASEPATH/core/Hooks.php
CI_Hooks是一个钩子类,是框架进行扩展的核心,能够在程序允许的各个阶段插入挂钩点,执行你自定义的类,函数等
06. BASEPATH/core/Config.php
配置文件管理类,加载读取或设置配置
07. BASEPATH/core/URI.php, BASEPATH/core/Router.php
URI类帮助你解析请求的uri,并提供分割uri的函数集合,供Router类使用
08. BASEPATH/core/Router.php
路由类,即通过请求的uri,和用户配置的路由(APPPATH/config/routes.php),将用户请求分发到指定的处理函数中(通常来说是某一个Controller实例中某一action函数)
09. BASEPATH/core/Output.php, BASEPATH/core/Input.php
输入类,即处理请求的输入参数,提供安全的获取方式。输出类将最后的执行结果发送出去,它还负责缓存的功能
10. BASEPATH/core/Controller.php
控制器基类,用单例模式对外提供实例,整个应用程序的心脏。它是一个Super Object,在应用内加载的类都可以成为控制器的成员变量,这一点非常重要,会在之后继续         讲到。
11. APPPATH/controllers/$RTR->fetch_directory().$RTR->fetch_class().'.php'
通过路由功能,得到控制器名,实例化真正的控制器类(子类)
12. BASEPATH/core/Loader.php
CI_Loader用于加载应用程序中的各种类库,模型,视图,数据库,文件等,并设置成为控制器的成员变量
13. call_user_func_array 调用处理函数
通过路由,得到action函数名,调用 Controller->action()函数,处理应用逻辑,实际业务处理逻辑便是在action函数中写的
14. $OUT->_display() 将内容输出

以上便是整个应用程序最基础的处理流程。下面选取核心内容代码再进行说明,以加强对CI的理解:

<&#63;php
//*BASEPATH/system/core/Common.php
	//引导文件中Benchmark,Hooks,Config等都是通过这个函数进行加载的
	function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
	{
		//记录加载过的类
		static $_classes = array();

		// 已经加载过,直接读取并返回
		if (isset($_classes[$class]))
		{
			return $_classes[$class];
		}

		$name = FALSE;

		// 在指定目录寻找要加载的类
		foreach (array(APPPATH, BASEPATH) as $path)
		{
			if (file_exists($path.$directory.'/'.$class.'.php'))
			{
				$name = $prefix.$class;

				if (class_exists($name) === FALSE)
				{
					require($path.$directory.'/'.$class.'.php');
				}

				break;
			}
		}

		// 没有找到
		if ($name === FALSE)
		{
			exit('Unable to locate the specified class: '.$class.'.php');
		}

		// 追踪记录下刚才加载的类,is_loaded()函数在下面
		is_loaded($class);

		$_classes[$class] = new $name();
		return $_classes[$class];
	}
	// 记录已经加载过的类。函数返回所有加载过的类
	function &is_loaded($class = '')
	{
		static $_is_loaded = array();

		if ($class != '')
		{
			$_is_loaded[strtolower($class)] = $class;
		}

		return $_is_loaded;
	}

//*BASEPATH/system/core/Controller.php
class CI_Controller {

	private static $instance;

	public function __construct()
	{
		self::$instance =& $this;
		
		//将所有在引导文件中(CodeIgniter.php)初始化的类对象(即刚才4,5,6,7,8,9等步骤),
		//注册成为控制器类的成员变量,就使得这个控制器成为一个超级对象(super object)
		foreach (is_loaded() as $var => $class)
		{
			$this->$var =& load_class($class);
		}
<span style="white-space:pre">		</span>//加载Loader对象,再利用Loader对象对程序内一系列资源进行加载<span style="white-space:pre">	</span>
		$this->load =& load_class('Loader', 'core');

		$this->load->initialize();
		
		log_message('debug', "Controller Class Initialized");
	}

	//这个函数对外提供了控制器的单一实例
	public static function &get_instance()
	{
		return self::$instance;
	}
}


//*BASEPATH/system/core/CodeIgniter.php
	// Load the base controller class
	require BASEPATH.'core/Controller.php';

	//通过这个全局函数就得到了控制器的实例,得到了这个超级对象,
	//意味着在程序其他地方调用这个函数,就能得到整个框架的控制权
	function &get_instance()
	{
		return CI_Controller::get_instance();
	}

	// 加载对应的控制器类
	// 注意:Router类会自动使用 router->_validate_request() 验证控制器路径
	if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
	{
		show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
	}

	include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');

	$class = $RTR->fetch_class(); //Controller class name
	$method = $RTR->fetch_method(); //action name

	//.....

	// 调用请求的函数
	// uri中除了class/function之外的段也会被传递给调用的函数
	call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));

	//输出最终的内容到浏览器
	if ($EXT->_call_hook('display_override') === FALSE)
	{
		$OUT->_display();
	}
	

//*BASEPATH/system/core/Loader.php
	//看一个Loader类加载model的例子。这里只列出了部分代码
	public function model($model, $name = '', $db_conn = FALSE)
	{
		$CI =& get_instance();
		if (isset($CI->$name))
		{
			show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
		}

		$model = strtolower($model);

		//依次根据model类的path进行匹配,如果找到了就加载
		foreach ($this->_ci_model_paths as $mod_path)
		{
			if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
			{
				continue;
			}

			if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
			{
				if ($db_conn === TRUE)
				{
					$db_conn = '';
				}

				$CI->load->database($db_conn, FALSE, TRUE);
			}

			if ( ! class_exists('CI_Model'))
			{
				load_class('Model', 'core');
			}

			require_once($mod_path.'models/'.$path.$model.'.php');

			$model = ucfirst($model);

			//这里依然将model对象注册成控制器类的成员变量。Loader在加载其他资源的时候也会这么做
			$CI->$name = new $model();

			$this->_ci_models[] = $name;
			return;
		}

		// couldn't find the model
		show_error('Unable to locate the model you have specified: '.$model);
	}

//*BASEPATH/system/core/Model.php
	//__get()是一个魔术方法,当读取一个未定义的变量的值时就会被调用
	//如下是Model基类对__get()函数的一个实现,使得在Model类内,可以像直接在控制器类内一样(例如$this->var的方式)去读取它的变量
	function __get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}
Copy after login

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 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles