在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
CodeIgniter.php可以说是CI的核心,大部分MVC的流程都是在这个文件夹中处理的,其中加载了很多外部文件,完成CI的一次完整流程。 首先是定义了CI的版本(此处为CI 2.2.0),接下来是连续require了2个文件,core/Common.php,config/constants.php,接下来是设置错误处理。 注: 1 CI的生命周期——system/core/CodeIgniter.php 2 3 include('core/commons.php') 4 载入 config/constant.php 5 其他配置 6 load benchmark,hooks 7 hook: pre_system 8 load Config, Utf8, URI, Router, Output 9 hook: cache_override 10 - output::_display_cache() 11 load Security, Input, Lang 12 hook: pre_controller 13 $CI = new $class() 14 hook: post_controller_constructor 15 hook: post_controller 16 hook: display_override 17 - Output::_display() 18 hook: post_system 19 清理数据库连接
源码分析如下: <?php /** * 详见 http://www.phpddt.com/tag/codeIgniter/ */ //如果入口文件系统目录常量BASEPATH没定义,就挂了 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //定义常量:CI_VERSION,CI_CORE define('CI_VERSION', '2.2.0'); define('CI_CORE', FALSE); /* *引入系统目录下core/Common.php,这里面全是CI的系统全局函数如is_php()、load_class()等 */ require(BASEPATH.'core/Common.php'); /* *加载应用目录下的常量,注意如果存在相应的开发环境目录,则加载对应目录下的文件,ENVIRONMENT实在入口文件定义的 */ if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php')) { require(APPPATH.'config/'.ENVIRONMENT.'/constants.php'); } else { require(APPPATH.'config/constants.php'); } /* *_exception_handler函数是刚才在Common.php中加载的函数,用来拦截错误,记录日志, * set_error_handler是在_exception_handler函数中用load_class加载过来的。 */ set_error_handler('_exception_handler'); //关闭magic quotes,关于这个的详细介绍请看:http://www.phpddt.com/php/php-magic-quotes.html if ( ! is_php('5.3')) { @set_magic_quotes_runtime(0); // Kill magic quotes } /* *子类前缀,很重要,也就是说你可以去扩展CI的核心类,后面说,默认配置应该是MY_ */ //看看你在index.php中是否定义前缀,没有定义的话就加载配置文件获知,get_config是Common.php中的全局函数 if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '') { get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix'])); } /* *php 程序运行默认是30s,这里用set_time_limt延长了,关于set_time_Limit() http://www.phpddt.com/php/set_time_limit.html * 扩展阅读,关于safe_mode:http://www.phpddt.com/php/643.html ,你会完全明白的 */ if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0) { @set_time_limit(300); } /* * 加载Benchmark,它很简单,就是计算任意两点之间程序的运行时间 */ $BM =& load_class('Benchmark', 'core'); $BM->mark('total_execution_time_start'); $BM->mark('loading_time:_base_classes_start'); //加载钩子,后期会分析到,这玩意特好,扩展它能改变CI的运行流程 $EXT =& load_class('Hooks', 'core'); //这里就是一个钩子啦,其实就是该钩子程序在这里执行 $EXT->_call_hook('pre_system'); //加载配置文件,这里面都是一些加载或获取配置信息的函数 $CFG =& load_class('Config', 'core'); // 如果在index.php中也有配置$assign_to_config,则也把它加入到$CFG if (isset($assign_to_config)) { $CFG->_assign_to_config($assign_to_config); } //加载utf8组件、URI组件、Router组件 $UNI =& load_class('Utf8', 'core'); $URI =& load_class('URI', 'core'); $RTR =& load_class('Router', 'core'); $RTR->_set_routing(); //如果在index.php中定义了$routing,那么就会覆盖上面路由 if (isset($routing)) { $RTR->_set_overrides($routing); } //加载output输出组件,不然你怎么用$this->Load->view()啊 $OUT =& load_class('Output', 'core'); //又见钩子,这里你可以自己写钩子程序替代Output类的缓存输出 if ($EXT->_call_hook('cache_override') === FALSE) { if ($OUT->_display_cache($CFG, $URI) == TRUE) { exit; } } //安全组件啦,防xss攻击啊,csrf攻击啊 //关于xss攻击:http://www.phpddt.com/php/php-prevent-xss.html //关于csrf:攻击:http://www.phpddt.com/reprint/csrf.html $SEC =& load_class('Security', 'core'); //加载输入组件,就是你常用的$this->input->post();等 $IN =& load_class('Input', 'core'); //加载语言组件啦 $LANG =& load_class('Lang', 'core'); //引入CI的控制器父类 require BASEPATH.'core/Controller.php'; function &get_instance() { return CI_Controller::get_instance(); } //当然你扩展了CI_Controller控制器的话,也要引入啦 if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php')) { require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'; } //加载你自己应用中的控制器Controller,如果没有当然error啦
|
2022-08-30
2022-08-17
2022-11-06
2022-07-18
2022-08-18
请发表评论