时间:2021-07-01 10:21:17 帮助过:49人阅读
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的理解:
输出最终的内容到浏览器 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; }
- <?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));
- //