带注释的源码如下,大致内容就是一些全局定义 目录结构 之类的,然后加载 Codeigniter.php 文件启动CI程序
<?php
/*
定义一个运行级别 或 运行环境
后面系统会根据不同的运行环境加载不同的配置文件
development 开启错误报告
test production 关闭错误报告
*/
define('ENVIRONMENT', 'development');
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'development':
error_reporting(E_ALL);
break;
case 'testing':
case 'production':
error_reporting(0);
break;
default:
exit('The application environment is not set correctly.');
}
}
/*
设置 系统的目录名称 and 应用程序的目录名称
*/
$system_path = 'system';
$application_folder = 'application';
/*
覆盖路由:
这里设置的route会覆盖整个系统识别出来的路由segments
*/
// $routing['directory'] = '';
// $routing['controller'] = '';
// $routing['function'] = '';
// 改变一些config选项的值
//$assign_to_config['name_of_config_item'] = 'value of config item';
if (defined('STDIN'))
{
chdir(dirname(__FILE__));// 命令行请求模式 切换工作目录到当前文件目录
}
// system path的路径存在 变量 $system_path 设置为绝对路径
if (realpath($system_path) !== FALSE)
{
$system_path = realpath($system_path).'/';
}
$system_path = rtrim($system_path, '/').'/';
if ( ! is_dir($system_path))
{//系统文件路径不是目录 退出
exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: ".pathinfo(__FILE__, PATHINFO_BASENAME));
}
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));//本文件文件名 index.php
define('EXT', '.php');//扩展名
define('BASEPATH', str_replace("\\", "/", $system_path));//将系统路径中的 \ 改成 /
define('FCPATH', str_replace(SELF, '', __FILE__));//本文件所在目录
define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));//系统目录
if (is_dir($application_folder))
{
define('APPPATH', $application_folder.'/');//application_floder 是目录 定义到APPPATH
}
else
{
// 不然 先尝试系统目录中是否存在application_floder
if ( ! is_dir(BASEPATH.$application_folder.'/'))
{
exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);
}
define('APPPATH', BASEPATH.$application_folder.'/');//定义在系统文件夹
}
//加载核心文件
require_once BASEPATH.'core/CodeIgniter.php';
设置 环境ENVIRONMENT 根据这个设置错误级别 error_reporting
可选的 routing 、 assign_to_config
系统路径、应用程序路径 以及对这些路径的检测
一些常量的定义
最后加载CodeIgniter.php
code上值得学习的小细节:
1、18 19行的case没有写break 这样两个case都会执行20行的代码(switch case是按顺序执行的)
2、realpath($path) 会检测path(通过与include_path 、当前工作目录(getcwd()) 进行拼接然后检测路径是否存在)
然后返回绝对路径