CI框架目录结构及运行机制

CI框架目录结构如下

CI
├─index.php 入口文件
├─system 框架程序目录
│ ├─core 框架的核心程序
│ │ ├─ CodeIgniter.php 引导性文件
│ │ ├─ Common.php 加载基类库的公共函数
│ │ ├─ Config.php 配置类:CI_Config
│ │ ├─Controller.php 基控制器类:CI_Controller
│ │ ├─ Exceptions.php 异常处理类
│ │ ├─ Hooks.php 钩子类
│ │ ├─ index.html
│ │ ├─ Input.php 输入类:CI_Input
│ │ ├─ Lang.php 加载语言包
│ │ ├─ Loader.php 加载类:CI_Loader
│ │ ├─ Model.php 基模型类:CI_Model
│ │ ├─ Output.php 输出类:CI_Output
│ │ ├─ Router.php 路由类:CI_Router
│ │ ├─ Security.php 安全处理
│ │ ├─ URI.php URL类:CI_URl
│ │ ├─ Utf8.php utf8 类
│ │
│ ├─helpers 辅助函数
│ │ ├─ cookie_helper.php cookie相关的辅助函数
│ │ ├─ date_helper.php 日期相关的辅助函数
│ │ ├─ directory_helper.php directory相关的辅助函数
│ │ ├─ download_helper.php 下载相关的辅助函数
│ │ ├─ email_helper.php email相关的辅助函数
│ │ ├─ file_helper.php 文件相关的辅助函数
│ │ ├─ form_helper.php 表单相关的辅助函数
│ │ ├─ html_helper.php html相关的辅助函数
│ │ ├─ index.html
│ │ ├─ path_helper.php 路径相关的辅助函数
│ │ ├─ string_helper.php 字符串相关的辅助函数
│ │ ├─ url_helper.php url相关的辅助函数,如:创建url的辅助函数
│ │
│ ├─language
│ │
│ └─libraries
│ ├─ Driver.php
│ ├─ Email.php
│ ├─ Encrypt.php
│ ├─ Form_validation.php
│ ├─ Image_lib.php
│ ├─ index.html
│ ├─ Log.php
│ ├─ Pagination.php
│ ├─ Session.php
│ └─ Upload.php

├─application 项目目录
│ ├─config 项目相关的配置
│ │ autoload.php 设置自动加载类库的配置文件
│ │ config.php 项目相关的配置文件
│ │ constants.php 常量配置文件
│ │ doctypes.php 文件类型配置文件
│ │ index.html
│ │ mimes.php
│ │ routes.php 路由配置文件
│ │
│ ├─controllers 控制器目录
│ │
│ ├─errors 错误提示模板
│ │ error_404.php
│ │ error_db.php
│ │ error_general.php
│ │ error_php.php
│ │ index.html
│ │
│ ├─helpers 项目的辅助函数
│ │ my_config_helper.php 创建自己的辅助函数
│ │
│ ├─logs 日志目录
│ ├─models 模型目录
│ └─views 视图目录

├─resources
│ └─XXX
│ ├─css
│ │ style.css
│ │
│ ├─images
│ │
│ └─js


运行机制详解

1.index.php :入口文件

define('ENVIRONMENT')//主要用于设置errors日志输出级别
$system_path//设置系统路径
//设置BASEPATH、FCPATH、SYSDIR、APPPATH等//设置路径信息变量,为加载相应文件信息准备
require_once BASEPATH.core/CodeIgniter.php//最后加载CodeIgniter.php作为总控制器

2.CodeIgniter.php加载过程,主要用于加载core核心目录下相应文件

require(BASEPATH.'core/Common.php'); //加载core目录下的Common文件,见2.1解析
require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');//加载constants目录,与开发环境无关时直接使用config目录下的constants目录
get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix'])); //设置子文件,扩展类的前缀
$BM =& load_class('Benchmark', 'core'); //加载benchmark类,mark记录当前的时间
$EXT =& load_class('Hooks', 'core'); //加载core目录下的Hooks钩子类
$EXT->_call_hook('pre_system'); //调用_call_hook(pre_system),根据pre_system内部调用_run_hook执行钩子,在系统开始正式工作前作预处理
$CFG =& load_class('Config', 'core');//继续执行core下的Config配置文件,
$CFG ->_assign_to_config($assign_to_config); 
$this ->set_item($key, $val);  //解析指定给config的配置文件,实质为对config[]赋值
$UNI =& load_class('Utf8', 'core'); //加载了UTF-8编码类,CI_Utf8
$URI =& load_class('URI', 'core');  //加载core目录的URI类,CI_URI
$RTR =& load_class('Router', 'core'); //设置route路由及覆盖信息,见2.2解析
_set_routing();
_set_overrides();
$OUT =& load_class('Output', 'core'); //实例化输出类,加载core目录下的output文件
$OUT->_display_cache($CFG, $URI) //判断是否存在页面缓存,是则输出文件
$SEC =& load_class('Security', 'core'); //加载core目录下的安全处理文件
$IN =& load_class('Input', 'core'); //实例化输入类,加载core目录下的input文件
$LANG =& load_class('Lang', 'core'); //加载语言类
require BASEPATH.'core/Controller.php';//加载基本控制器类,见2.3解析
require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';//尝试加载扩展的自定义子类控制器
include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');//加载自定义控制器下的控制器类
$BM->mark('loading_time:_base_classes_end');//设定一个benchmark测试点
$class  = $RTR->fetch_class();//分别获取uri地址的控制器类名和方法名
$method = $RTR->fetch_method();
if ( ! class_exists($class) //判断方法及类是否合理
    OR strncmp($method, '_', 1) == 0
    OR in_array(strtolower($method),
        array_map('strtolower', 
            get_class_methods('CI_Controller')
        )
    )
)
    $EXT->_call_hook('pre_controller'); //处理器执行前进行预处理,并做benchmark设置
$CI = new $class();//获取执行的控制器实例,实例化构造器
$EXT->_call_hook('post_controller_constructor');//实例化控制器类后的钩子处理
if (method_exists($CI, '_remap'))
    $CI->_remap($method, array_slice($URI->rsegments, 2))//如果控制器存在_remap()方法,则执行, 判断条件$CI为控制器类 
else//判断方法在类当中的存在似,如果不存在,则设置
    call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));//最终传递参数供调用控制类方法
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');//benchmark标记时间结束点
$EXT->_call_hook('post_controller');//控制器生存周期,在控制器执行完成后执行后续操作
$OUT->_display(); //输出页面进行展示
$EXT->_call_hook('post_system'); //请求生存周期完成后的终结操作
$CI->db->close();//自动关闭数据库资源

2.1 Core/Common.php加载

function is_php($version)//用于比较版本号的函数
function is_really_writable($file)//用于判断是否可以写文件,在不同的系统中可靠程度不同,W中通过判断is_readonly,U中如果safe_mode为开则不确定性
function load_class($class, $directory = 'libraries', $prefix = 'CI_')//用于加载目录下的PHP文件的class类
foreach (array(APPPATH, BASEPATH) as $path)//分别在application和system目录下轮循
file_exists($path.$directory.'/'.$class.'.php');//找到对应的PHP文件
require($path.$directory.'/'.$class.'.php'); //require加载对应的PHP文件内的类,加了前缀,此处可扩展
break;  //如正确加载则退出,否则继续尝试加载文件 
file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')  ;//自扩展的class类,如My_Test
if ($name === FALSE)   //如果$name不存在,则exit()退出 ,(在自定义类加载时,此处可作为扩展点,增加边际条件)
is_loaded($class);     //确类已经加载
$_classes[$class] = new $name();  //加载至静态的classes数祖中,用于缓存,调用时首先从classes中获取
function is_loaded($class = '') ;                     
//设置$_is_loaded数祖,参数$class不为空,判断是否存在gf $_is_loaded,否则设置
function &get_config($replace = array());//用于获取Config的实例化文件
static $_config;       //定义config类型
$file_path = APPPATH.'config/config.php';      //确定application目录路径下定义的config.php的路径
require($file_path);   //加载application/config/config.php类
count($replace) > 0  ;  //对于config.php中定义的变量,如果有replace,则逐个替代
foreach ($replace as $key => $val)
$config[$key] = $val;
return $_config[0] =& $config;   //最后返回定义的config的结果集
function config_item($item)    //配置选项,从config的数祖对象中返还特殊的配置项
$config =& get_config();
$_config_item[$item] = $config[$item];
function show_error            //用于错误信息输出
$_error =& load_class('Exceptions', 'core');    //加载Exceptions类
echo $_error->show_error($heading, $message, 'error_general', $status_code);  //直接输出错误
function show_404              //用于输出404页面,输出的错误信息页面可配置
function log_message           //用于写日志信息
$_log =& load_class('Log');
$_log->write_log($level, $message, $php_error);


function set_status_header     //用于输出状态的heade信息
function _exception_handler
function remove_invisible_characters
function html_escape           //过滤HTML变量
return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));

2.2Router路由信息设置

_set_routing() ;
$segments = array();    //根据目录,控制器,函数的触发器设定segment[]的uri段值,分别fetch()方法去取对象值
include(APPPATH.'config/routes.php');       //加载config下的routes文件
$this->routes          //设置routes数祖值,从config的route中获取
$this->default_controller       //设置routes的控制器值,从config的route中获取
return $this->_validate_request($segments); //验证uri的segment合法性
$this->uri->_remove_url_suffix();$this->uri->_reindex_segments();  //进一步清理解析uri,使segment从1开始x
_set_overrides()  //根据$routing的值,重新设定directory、controller、function参数
$this->set_directory($routing['directory']);
$this->set_class($routing['controller']);
$this->set_method($routing['function']);

2.3 core/Controller.php加载

__construct()                                         //构造函数
self::$instance =& $this;  
foreach (is_loaded() as $var => $class)       //根据is_loaded()的信息加载相应的类
$this->$var =& load_class($class);   
$this->load =& load_class('Loader', 'core');  //加载core/Loader的php文件
$this->load->initialize();                    //主要用于autoload加载信息,如libraries、database等等
function &get_instance                                //返回当前实例
return self::$instance

扩展点:PHP自动加载机制在CodeIgniter中的应用
1.PHP自动加载机制:PHP5后,提供了类的自动加载机制,即类在加载时才被使用,即Lazy loading,共有二种方式
1.1: __autoload()通过扩展可实现,实质为设定规则加载相应路径的PHP文件(require、include方式)
1.2: 将autoload_func指向php文件,这个一般用c语言扩展实现
详见:http://blog.csdn.net/qq_22253823/article/details/45288069

2.在CodeIgniter中的应用
根据上述源码分析可知:CodeIgniter中所有的操作都是以Controller为起始,只需在Cotroller加载的过程中,
使__autoload()函数自动加载即可,目前的加载方式为在application/config/config.php中设置__autoload()
函数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值