http://blog.163.com/wu_guoqing/blog/static/196537018201281672320862/
<?php
//入口文件(index.php)
/* 定义了CodeIgniter框架的目录和我们的项目目录
*---------------------------------------------------------------
* APPLICATION ENVIRONMENT
*---------------------------------------------------------------
*
* You can load different configurations depending on your
* current environment. Setting the environment also influences
* things like logging and error reporting.
*
* This can be set to anything, but default usage is:
*
* development
* testing
* production
*
* NOTE: If you change these, also change the error_reporting() code below
*
*/
define('ENVIRONMENT', 'development');
//配置项目运行的环境,该配置会影响错误报告的显示和配置文件的读取。
/*
函数定义一个常量
在设定以后,常量的值无法更改
常量名不需要开头的美元符号 ($)
作用域不影响对常量的访问
常量值只能是字符串或数字
*/
/*
*---------------------------------------------------------------
* ERROR REPORTING
*---------------------------------------------------------------
*
* Different environments will require different levels of error reporting.
* By default development will show errors but testing and live will hide them.
*/
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
case 'development':
error_reporting(E_ALL); //报告所有错误
//设置 PHP 的报错级别并返回当前级别(http://www.w3school.com.cn/php/func_error_reporting.asp)
break;
case 'testing':
case 'production':
error_reporting(0); //禁用错误报告
//报告运行时错误 error_reporting(E_ERROR | E_WARNING | E_PARSE);
break;
default:
exit('The application environment is not set correctly.');
}
}
/*
*---------------------------------------------------------------
* SYSTEM FOLDER NAME
*---------------------------------------------------------------
*
* This variable must contain the name of your "system" folder.
* Include the path if the folder is not in the same directory
* as this file.
*
*/
$system_path = 'system';//CodeIgniter框架的目录,可自由更改。 设置系统目录
/*
*---------------------------------------------------------------
* APPLICATION FOLDER NAME
*---------------------------------------------------------------
*
* If you want this front controller to use a different "application"
* folder then the default one you can set its name here. The folder
* can also be renamed or relocated anywhere on your server. If
* you do, use a full server path. For more info please see the user guide:
* http://codeigniter.com/user_guide/general/managing_apps.html
*
* NO TRAILING SLASH!
*
*/
$application_folder = 'application'; //定义我们的应用程序目录 设置应用目录
/*
* --------------------------------------------------------------------
* DEFAULT CONTROLLER
* --------------------------------------------------------------------
*
* Normally you will set your default controller in the routes.php file.
* You can, however, force a custom routing by hard-coding a
* specific controller class/function here. For most applications, you
* WILL NOT set your routing here, but it's an option for those
* special instances where you might want to override the standard
* routing in a specific front controller that shares a common CI installation.
*
* IMPORTANT: If you set the routing here, NO OTHER controller will be
* callable. In essence, this preference limits your application to ONE
* specific controller. Leave the function name blank if you need
* to call functions dynamically via the URI.
*
* Un-comment the $routing array below to use this feature
*
*/
/*
下面这个地方设置的$routing会对路由有重定向的作用。详见:core/CodeIginter.php
*/
// The directory name, relative to the "controllers" folder. Leave blank
// if your controller is not in a sub-folder within the "controllers" folder
// $routing['directory'] = '';
// The controller class file name. Example: Mycontroller
// $routing['controller'] = '';
// The controller function you wish to be called.
// $routing['function'] = '';
/*
* -------------------------------------------------------------------
* CUSTOM CONFIG VALUES
* -------------------------------------------------------------------
*
* The $assign_to_config array below will be passed dynamically to the
* config class when initialized. This allows you to set custom config
* items or override any default config values found in the config.php file.
* This can be handy as it permits you to share one application between
* multiple front controller files, with each file containing different
* config values.
*
* Un-comment the $assign_to_config array below to use this feature
*/
/*下面这里提供一个定义配置信息的地方。其实在index.php里面很多地方都可以设置一些配置,像刚才上面的$routing,
而在而在这里设置的配置信息要优先于在config/目录下设置的配置信息。*/
// $assign_to_config['name_of_config_item'] = 'value of config item';
// --------------------------------------------------------------------
// END OF USER CONFIGURABLE SETTINGS. DO NOT EDIT BELOW THIS LINE
// --------------------------------------------------------------------
/*
* ---------------------------------------------------------------
* Resolve the system path for increased reliability
* ---------------------------------------------------------------
*/
// Set the current directory correctly for CLI requests
//define('STDIN','test'); //wjx
if (defined('STDIN')) //defined() 函数检查某常量是否存在。 若常量存在,则返回 true,否则返回 false。
{//这个请参考:http://blog.163.com/wu_guoqing/blog/static/196537018201272512616394/
///echo __FILE__; //wjx
chdir(dirname(__FILE__));
/*chdir()函数把当前的目录改变为指定的目录
dirname()函数返回路径中的目录部分(去掉文件名后的目录名)。
__FILE__当前运行文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。这是一个魔法变量(预定义常量)
*/
}
//计算出$system_path,即核心文件所在的路径。realpath($path)中的$path必须为存在的路径。
if (realpath($system_path) !== FALSE)
{/*realpath()函数返回绝对路径,该函数删除所有符号连接(比如 '/./', '/../' 以及多余的 '/'),返回绝对路径名。
--> echo realpath("test.txt"); 输出: C:\Inetpub\testweb\test.txt
*/
$system_path = realpath($system_path).'/';
}
// ensure there's a trailing slash 确保以/结尾
$system_path = rtrim($system_path, '/').'/';//chop() 函数从字符串的末端开始删除空白字符或其他预定义字符(eg: \n\n)。
// Is the system path correct? //判断是否为正确的目录
if ( ! is_dir($system_path)) //is_dir()函数的结果会被缓存。请使用 clearstatcache() 来清除缓存
{
exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: ".pathinfo(__FILE__, PATHINFO_BASENAME));
}
/*
* -------------------------------------------------------------------
* Now that we know the path, set the main path constants
* 在上面配置好一些文件目录信息后,根据这些目录来定义好一些常量。
* -------------------------------------------------------------------
*/
// The name of THIS file 这个入口文件的文件名,目前是index.php
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));//pathinfo()函数以数组的形式返回文件路径的信息
/*
Array
(
[dirname] => /testweb
[basename] => test.txt
[extension] => txt
)
*/
// The PHP file extension
// this global constant is deprecated. 文件扩展名
define('EXT', '.php');
// Path to the system folder 统一以/为目录分隔符(windows下是/或\,linux下默认是/)
define('BASEPATH', str_replace("\\", "/", $system_path));
/*
str_replace() 函数使用一个字符串替换字符串中的另一些字符 str_replace(find,replace,string,count(可选))
eg1:
echo str_replace("world","John","Hello world!"); 输出:Hello John!
eg2:
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
输出:
Array(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Replacements: 1
*/
// Path to the front controller (this file) 前端控制器所在的目录。在CI里面就是这个入口文件。
define('FCPATH', str_replace(SELF, '', __FILE__));
// Name of the "system folder"
// 取得核心文件的目录名,具体做法如下:
//trim(BASHPATH,'/'):先把BASHPATH给修剪一下,去掉首尾的‘/’
//先后通过strrchr(xxx,'/'):把上述得出来的字符串,截取出以最后一个'/'开头到结尾的一个子字符串。
//最后再trim(xxx,'/'),去掉两端的‘/’,实质这里是去掉左边的'/'。
define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));
//trim()函数从字符串的两端删除空白字符和其他预定义字符
//strrchr()函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。
// The path to the "application" folder 定义应用文件目录
//检查并定义应用程序目录
if (is_dir($application_folder))//在这里直接是一个文件夹
{//先看看这个被你配置的目录是不是相对于当前文件,如果是的话,就可以成功定义。
define('APPPATH', $application_folder.'/');
}
else
{//如果不是,则再判断相对于核心文件目录是否存在这个应用目录。如果没有就拉倒了。。
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.'/');
}
/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
//实质上,这个前端控制器兼入口文件仅仅是定义当前项目运行的环境和一些常量,
//而真正起宏观控制作用的是下面这个CodeIgniter.php。
require_once BASEPATH.'core/CodeIgniter.php'; //引入CodeIgniter框架的核心文件
//如果你导入的页面不存在的话include_once是可以执行的,而require_once是不能执行的,这个事区别
/* End of file index.php */
/* Location: ./index.php */
/**
* 总结一下这个文件做了一些什么:
* 第一,先设置好当前项目的运行环境,这里主要是错误报告方面的设置,这个放在了整个项目运行的第一位。
* 第二,再配置好一些目录信息,这些都是一定开发人员可以自定义的东西。然后根据配置目录信息,CI会把一些以后会有用的东西定义为常量
* ,为什么要这样做呢?因为以后会在很多不同地方,例如CI里面的各个组件都会用到路径相关的信息,在这里统一计算并定义,以后方
* 便引用和修改。
* 第三,引入CodeIgniter.php进行工作。
* 移步至核心文件目录下的(点击:)core/CodeIgniter.php...
*/