codeigniter(以下简称ci,官网以及唐山大地震的观后感)是一个流行的php框架,小巧但功能强大,简洁轻量同时拥有很好的扩展性,在国内也比较受欢迎。另一方面,ci却没有与时俱进,并不支持php5.3之后的一些特性,导致它相对更适合较老一些的项目。虽然如此,ci仍是一个优秀的框架,而且它本身内核较小,源码优雅,适于学习。
ci易于使用,可以方便的开发出web应用。先来看一下ci的工作流程图(此处内容引用自)
、
1.index.php 作为前端控制器,初始化运行 codeigniter 所需要的基本资源。
2.router 检查 http 请求,以确定谁来处理请求。
3.如果缓存(cache)文件存在,它将绕过通常的系统执行顺序,被直接发送给浏览器。
4.安全(curity)。应用程序控制器(application controller)装载之前,http 请求和任何用户提交的数据将被过滤。
5.控制器(controller)装载模型、核心库、辅助函数,以及任何处理特定请求所需的其它资源。
6.最终视图(view)渲染发送到 web 浏览器中的内容。如果开启缓存(caching),视图首先被缓存,所以将可用于以后的请求。
以上给出了一个大致流程。那么当看到页面在浏览器中呈现时,程序内部究竟是如何工作的呢?
下面按照执行顺序,依次列出了ci框架主要加载的文件,并简要介绍其作用:
01. index.php
定义使用环境(environment),框架路径(system_path,bapath),应用目录(application_folder),应用路径(apppath)等,加载(require)ci核心文件
02. bapath/core/codeigniter.php (ps.实际上bapath包含最后的文件分隔符’/’,这里额外加上了’/’是为了更清晰的展示)
系统初始化文件,整个框架最核心的部分,在此加载(load)了一系列的ba class,并且执行这次请求
03. bapath/core/common.php
common文件包含一系列的基础和公共函数 ,供全局使用,例如load_class(),get_config()等
04. bapath/core/benchmark
这是一个基准测试类,默认标注了应用各个阶段的执行点,以得到其执行时间。也允许你自己定义监测点。
05. bapath/core/hooks.php
ci_hooks是一个钩子类,是框架进行扩展的核心,能够在程序允许的各个阶段插入挂钩点,执行你自定义的类,函数等
06. bapath/core/config.php
配置文件管理类,加载读取或设置配置
07. bapath/core/uri.php, bapath/core/router.php
uri类帮助你解析请求的uri,并提供分割uri的函数集合,供router类使用
08. bapath/core/router.php
路由类,即通过请求的uri,和用户配置的路由(apppath/config/routes.php),将用户请求分发到指定的处理函数中(通常来说是某一个controller实例中某一action函数)
09. bapath/core/output.php, bapath/core/input.php
输入类,即处理请求的输入参数,提供安全的获取方式。输出类将最后的执行结果发送出去,它还负责缓存的功能
10. bapath/core/controller.php
控制器基类,用单例模式对外提供实例,整个应用程序的心脏。它是一个super object,在应用内加载的类都可以成为控制器的成员变量,这一点非常重要,会在之后继续 讲到。
11. apppath/controllers/$rtr->fetch_directory().$rtr->fetch_class().’.php’
通过路由功能,得到控制器名,实例化真正的控制器类(子类)
12. bapath/core/loader.php
ci_loader用于加载应用程序中的各种类库,模型,视图,数据库,文件等,并设置成为控制器的成员变量
13. call_ur_func_array 调用处理函数
通过路由,得到action函数名,调用 controller->action()函数,处理应用逻辑,实际业务处理逻辑便是在action函数中写的
14. $out->_display() 将内容输出
以上便是整个应用程序最基础的处理流程。下面选取核心内容代码再进行说明,以加强对ci的理解:
<?php//*bapath/system/core/common.php//引导文件中benchmark,hooks,config等都是通过这个函数进行加载的function &load_class($class, $directory = 'libraries', $prefix = 'ci_'){//记录加载过的类static $_class = array();// 已经加载过,直接读取并返回if (ist($_class[$class])){return $_class[$class];}$name = fal;// 在指定目录寻找要加载的类foreach (array(apppath, bapath) as $path){if (file_exists($path.$directory.'/'.$class.'.php')){$name = $prefix.$class;if (class_exists($name) === fal){require($path.$directory.'/'.$class.'.php');}break;}}// 没有找到if ($name === fal){exit('unable to locate the specified class: '.$class.'.php');}// 追踪记录下刚才加载的类,is_loaded()函数在下面is_loaded($class);$_class[$class] = new $name();return $_class[$class];}// 记录已经加载过的类。函数返回所有加载过的类function &is_loaded($class = ''){static $_is_loaded = array();if ($class != ''){$_is_loaded[strtolower($class)] = $class;}return $_is_loaded;}//*bapath/system/core/controller.phpclass ci_controller {private static $instance;public function __construct(){lf::$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 lf::$instance;}}//*bapath/system/core/codeigniter.php// load the ba controller classrequire bapath.'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. plea 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_ur_func_array(array(&$ci, $method), array_slice($uri->rgments, 2));//输出最终的内容到浏览器if ($ext->小帅哥头像;_call_hook('display_override') === fal){$out-&gqq空间签名档t;_display();}//*bapath/system/core/loader.php//看一个loader类加载model的例子。这里只列出了部分代码public function model($model, $name = '', $db_conn = fal){$ci =& get_instance();if (ist($ci->$name)){show_error('the model name you are loading is the name of a resource that is already being ud: '.$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 !== fal and ! class_exists('ci_db')){if ($db_conn === true){$db_conn = '';}$ci->load->databa($db_conn, fal, 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 modelshow_error('unable to locate 约束反力the model you have specified: '.$model);}//*bapath/sy乔奎恩 古兹曼stem/core/model.php//__get()是一个魔术方法,当读取一个未定义的变量的值时就会被调用//如下是model基类对__get()函数的一个实现,使得在model类内,可以像直接在控制器类内一样(例如$this->var的方式)去读取它的变量function __get($key){$ci =& get_instance();return $ci->$key;}
本文发布于:2023-04-06 19:20:42,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/ceaac65eada3deaafc64d967cce8287e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP CodeIgniter框架的工作原理研究.doc
本文 PDF 下载地址:PHP CodeIgniter框架的工作原理研究.pdf
留言与评论(共有 0 条评论) |