CodeIgniter 核心代码阅读-程序启动文件CodeIgniter.php

CodeIgniter.php----加载需要的类库,CI框架所有操作都在这里执行

一个简单的页面需要加载的类库如下所示:

    [0] => D:\wamp\www\CodeIgniter_2.1.3\system\core\CodeIgniter.php
    [1] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Common.php
    [2] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Benchmark.php
    [3] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Hooks.php
    [4] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Config.php
    [5] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Utf8.php
    [6] => D:\wamp\www\CodeIgniter_2.1.3\system\core\URI.php
    [7] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Router.php
    [8] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Output.php
    [9] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Security.php
    [10] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Input.php
    [11] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Lang.php
    [12] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Controller.php
    [13] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Loader.php
    [14] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Model.php
    [15] => D:\wamp\www\CodeIgniter_2.1.3\system\database\DB.php
    [16] => D:\wamp\www\CodeIgniter_2.1.3\system\database\DB_driver.php
    [17] => D:\wamp\www\CodeIgniter_2.1.3\system\database\DB_active_rec.php
    [18] => D:\wamp\www\CodeIgniter_2.1.3\system\database\drivers\mysql\mysql_driver.php
    [19] => D:\wamp\www\CodeIgniter_2.1.3\application\config\constants.php
    [20] => D:\wamp\www\CodeIgniter_2.1.3\application\config\config.php
    [21] => D:\wamp\www\CodeIgniter_2.1.3\application\config\routes.php
    [22] => D:\wamp\www\CodeIgniter_2.1.3\application\config\mimes.php
    [23] => D:\wamp\www\CodeIgniter_2.1.3\application\controllers\news.php
    [24] => D:\wamp\www\CodeIgniter_2.1.3\application\config\autoload.php
    [25] => D:\wamp\www\CodeIgniter_2.1.3\application\models\news_model.php
    [26] => D:\wamp\www\CodeIgniter_2.1.3\application\config\database.php

CodeIgniter核心类库加载顺序:

Common.php->Benchmark.php->Hooks.php->Config.php->Utf8.php->URI.php->Router.php->Output.php->Securit.php->Input.php->Lang.php->Controller.php->Loader.php

CodeIgniter.php源码:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//ci版本号
define('CI_VERSION', '2.1.3');

//ci分支
define('CI_CORE', FALSE);

//加载全局函数
require(BASEPATH.'core/Common.php');

//加载框架定义的常量,可在APPPATH.'config/constants.php'文件中定义常量

if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
{
	require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
}
else
{
	require(APPPATH.'config/constants.php');
}

//定义错误处理句柄
set_error_handler('_exception_handler');

//关闭魔术引号,本特性已自 PHP 5.3.0 起废弃并将自 PHP 5.4.0 起移除。
if ( ! is_php('5.3'))
{
	@set_magic_quotes_runtime(0); // Kill magic quotes
}

//设置类前缀名
if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
{
	get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
}

//设置PHP脚本的最大执行时间
if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
{
	@set_time_limit(300);
}

//实例化计时类
$BM =& load_class('Benchmark', 'core');
$BM->mark('total_execution_time_start');
$BM->mark('loading_time:_base_classes_start');

//实例化钩子类
$EXT =& load_class('Hooks', 'core');

//钩子--仅仅在benchmark 和 hooks 类 加载完毕的时候. 没有执行路由或者其它的过程.
$EXT->_call_hook('pre_system');

//实例化config类
$CFG =& load_class('Config', 'core');

// Do we have any manually set config items in the index.php file?
if (isset($assign_to_config))
{
	$CFG->_assign_to_config($assign_to_config);
}

//实例化UTF-8类
$UNI =& load_class('Utf8', 'core');

//实例化URI类
$URI =& load_class('URI', 'core');

//实例化Router类,并设置路由
$RTR =& load_class('Router', 'core');
$RTR->_set_routing();
// Set any routing overrides that may exist in the main index file
if (isset($routing))
{
$RTR->_set_overrides($routing);
}
//实例化Output类
$OUT =& 
load_class('Output', 'core');
//是否已经缓存了文件
if ($EXT->_call_hook('cache_override') === FALSE)
{
       if ($OUT->_display_cache($CFG, $URI) == TRUE)
       {
           exit;
       }
}
//实例化Security类
$SEC =& load_class('Security', 'core');
//实例化Input类
$IN =& load_class('Input', 'core');
//实例化Lang类
$LANG =& load_class('Lang', 'core');

//加载CI_Controller类
require BASEPATH.'core/Controller.php';

function &get_instance()
{
	return CI_Controller::get_instance();
}

//加载应用程序Controller类
if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
{
	require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
}
	
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');

// Set a mark point for benchmarking
$BM->mark('loading_time:_base_classes_end');

//安全检查如果控制器不存在或者方法名不存在,则显示404
//控制器类名
$class  = $RTR->fetch_class();
//控制器方法名
$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')))
	)
{
	if ( ! empty($RTR->routes['404_override']))
	{
		$x = explode('/', $RTR->routes['404_override']);
		$class = $x[0];
		$method = (isset($x[1]) ? $x[1] : 'index');
		if ( ! class_exists($class))
		{
			if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
			{
				show_404("{$class}/{$method}");
			}

			include_once(APPPATH.'controllers/'.$class.'.php');
		}
	}
	else
	{
		show_404("{$class}/{$method}");
	}
}

//钩子--在调用任何控制器之前调用.此时所用的基础类,路由选择和安全性检查都已完成.
$EXT->_call_hook('pre_controller');

//实例化请求的控制器
// Mark a start point so we can benchmark the controller
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');

$CI = new $class();

//钩子--在控制器实例化之后,任何方法调用之前调用.
$EXT->_call_hook('post_controller_constructor');

//执行请求的方法
//如果控制器中定义了_remap方法,则执行remap方法
if (method_exists($CI, '_remap'))
{
	$CI->_remap($method, array_slice($URI->rsegments, 2));
}
else
{
	// is_callable() returns TRUE on some versions of PHP 5 for private and protected
	// methods, so we'll use this workaround for consistent behavior
	if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
	{
		// Check and see if we are using a 404 override and use it.
		if ( ! empty($RTR->routes['404_override']))
		{
			$x = explode('/', $RTR->routes['404_override']);
			$class = $x[0];
			$method = (isset($x[1]) ? $x[1] : 'index');
			if ( ! class_exists($class))
			{
				if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
				{
					show_404("{$class}/{$method}");
				}

				include_once(APPPATH.'controllers/'.$class.'.php');
				unset($CI);
				$CI = new $class();
			}
		}
		else
		{
			show_404("{$class}/{$method}");
		}
	}

	// Call the requested method.
	// Any URI segments present (besides the class/function) will be passed to the method for convenience
	call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
}


// Mark a benchmark end point
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');

//钩子--在控制器完全运行之后调用.
$EXT->_call_hook('post_controller');

//输出显示内容
if ($EXT->_call_hook('display_override') === FALSE)
{
	$OUT->_display();
}

//钩子--在最终渲染页面发送到浏览器之后,浏览器接收完最终数据的系统执行末尾调用
$EXT->_call_hook('post_system');

//关闭数据路连接
if (class_exists('CI_DB') AND isset($CI->db))
{
	$CI->db->close();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
压缩包里有两个文件: 1、CodeIgniter:php敏捷开发框架 2、CodeIgniter 1.7.1 用户指南 本书详细讲解了 CI 的一些主要特性。本书并不包含 CI 的所有内容和全部细节。CI 有一本出色的在线《用户指南》,它详细讲解了大多数的内容。它可以与 CI 一起下载。 本书并不想重复《用户指南》中的内容。相反,本书试图让你轻松了解 CI 框架是如何工作的,那么,你可以先决定它是否对你有价值,然后再阅读本书。 在试图解释 CI 是如何工作时,本书的某些内容已经超出了《用户指南》的范围。(《用户指南》更注重实际应用。)这意味着在“实战训练”中有一些非常理论化的章节。我发现这有助于理解 CI 内部的运行机制;否则,当你遇到令人费解的错误消息时就不容易解决。 我尝试在展示 CI 代码段时使用一个“真实世界”的例子。我想展示的是,CI 可以用于开发一个正式的网站。目前,我手头上有几个正在运行的客户网站,我希望依照我指定的方式去对其进行检测控制以及测试,同时记录下程序操作行为,在我需要时我可以得到一份相关的报告。 本书中的范例无法将 CI 的功能一丝不漏的完全展示,但我想这些范例应当还是在一定程度上展现了 CI 在简化处理常用应用(以及一些非常用应用)上的能力。 本书系统地讲解了 CodeIgniter 的主要特性,并配合相应的代码范例进行了详尽的解释,使你能够由浅入深地掌握 CodeIgniter
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值