ci框架源码阅读笔记2 一切的入口 index.php,ciindex.php
上一节(ci框架源码阅读笔记1 - 环境准备、基本术语和框架流程)中,我们提到了ci框架的基本流程,这里再次贴出流程图,以备参考:
作为CI框架的入口文件,源码阅读,自然由此开始。在源码阅读的过程中,我们并不会逐行进行解释,而只解释核心的功能和实现。
1. 设置应用程序环境
这里的development可以是任何你喜欢的环境名称(比如dev,再如test),相对应的,你要在下面的switch case代码块中,对设定的环境做相关的错误控制,否则,CI框架会认为你没有配置好相应的环境,从而退出进程并给出对应的错误信息:
为什么一开始就要配置ENVIRONMENT?这是因为,CI框架中很多组件都依赖于ENVIRONMENT的配置,我们看一下system中,引用ENVIRONMENT的地方:
可以看到,很多组件都依赖于ENVIRONMENT.例如,查看system/config/Common.php, 这其中有一段引入配置文件的代码,是这样实现的:
立即学习
“
PHP免费学习笔记(深入)
”;
在CI框架中,很多配置文件都是通过这种方式引入的,因此ENVRIONMENT对于CI框架的正确运行时必须的,所以需要在开始的时候配置好ENVIRONMENT。
设置ENVIRONMENT的一个好处是:可以很方便的切换系统的配置而不必修改系统代码。例如,在系统进入测试阶段时,database配置为测试的数据库,而在系统测试完毕时,database切换到线上的数据库。这好比是用一个开关控制了系统的环境切换,自然是非常方便的。
2. 配置系统目录和应用程序目录
CI框架允许你将系统核心源码和应用程序代码分开放置,但是你必须设定好系统的system文件夹和application文件夹(同样,文件夹名字可以是任何合法的文件夹名称,而不一定使用’system’和’application’):
接下来,有这么一段代码:
这段代码是干嘛的呢?首先,STDIN、STDOUT、STDERR是PHP以 CLI(Command Line Interface)模式运行而定义的三个常量,这三个常量类似于Shell的stdin,stdout,stdout,分别是PHP CLI模式下的标准输入、标准输出和标准错误流。也就是说,这三行代码是为了保证命令行模式下,CI框架可以正常运行。关于PHP CLI的更多细节可以参考:http://www.php-cli.com/
3. system目录的正确性验证和application目录验证
(1). system目录的正确性验证
Realpath返回的是目录或文件的绝对目录名(没有最后的/)
几个定义的常量(PATH结尾的常量表示目录路径,DIR结尾的变量表示目录名):
a.SELF(这里指index.php文件)
b.EXT(deprecated,废弃的,不必关注)
c.BASEPATH(system文件夹的路径)
d.FCPATH(前端控制器的路径)
e.SYSDIR(系统system目录名)
f.APPPATH(应用程序路径)
查看所有定义的常量的方法:
(2)application的目录验证。
代码较简单,不做过多的解释:
入口文件的最后一行,引入CodeIgniter.php(也是下一步阅读的关键)。CodeIgniter.php被称为bootstrap file,也就是它是一个引导文件,是CI框架执行流程的核心文件。
总结一下,index.php并没有做太多复杂的工作,而是类似一个后勤,为CI框架的运行提供了一系列配置参数和正确性验证,而这些配置和验证,是CI框架能够正常运行的关键。
最后,按照惯例,贴一下整个文件的源码(简化注释版):
php的ci框架CodeIgniter目录的规划问题
阁下需要在使用框架,那么就要把项目里的第一个程序都放在框架架构之中,而不能在根目录下新建一个admin.php。
阁下应该知晓,CI框架的入口文件是index.php,里面的任何页面都应该基于这个入口文件,即访问路径永远是index.php/*****这样的形式,而不能单独出来一个admin.php,这样的话,没有通过入口文件访问了,那么框架的效用也就没有了。
所以,阁下应该在application里的controllers目录下建一个admin.php,并按CI框架控制器的规则来使用它,这样,访问路径就是index.php/admin这样了
当然,阁下会以为所有的URL中都有一个index.php非常难看,那么阁下可以通过CI框架的路由规则将之隐藏掉,也可以使用服务器的伪静态功能来隐藏掉。但也仅是隐藏了而已,实际路径仍然有index.php这个入口文件。
CI框架怎删除地址栏的 indexphp
1.修改Http.conf的LoadModule rewrite_module modules/mod_rewrite.so去掉注释 2.ci根目录增加.htaccess文件 RewriteEngine On RewriteBase /ci #Removes access to the system folder by users. #Additionally this will allow you to create a System.php controller, #previously this would not have been possible. #'system' can be replaced if you have renamed your system folder. RewriteCond %{REQUEST_URI} ^system.* RewriteRule ^(.*)$ /index.php?/$1 [L] #When your application folder isn't in the system folder #This snippet prevents user access to the application folder #Submitted by: Fabdrol #Rename 'application' to your applications folder name. RewriteCond %{REQUEST_URI} ^application.* RewriteRule ^(.*)$ /index.php?/$1 [L] #Checks to see if the user is attempting to access a valid file, #such as an image or css document, if this isn't true it sends the #request to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L] # If we don't have mod_rewrite installed, all 404's # can be sent to index.php, and everything works as normal. # Submitted by: ElliotHaughin ErrorDocument 404 /index.php .htaccess文件内容
define('ENVIRONMENT', 'development');default: exit('The application environment is not set correctly.');if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
{
$file_path = APPPATH.'config/config.php';
}$system_path = 'system';
$application_folder = 'application';if (defined('STDIN'))
{
chdir(dirname(__FILE__));
}if (realpath($system_path) !== FALSE)
{
$system_path = realpath($system_path).'/';
}
$system_path = rtrim($system_path, '/').'/';
if ( ! is_dir($system_path))
{
exit("xxxxxxxx");
}Print_r(get_defined_constants());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.'/');
}require_once BASEPATH.'core/CodeIgniter.php'; 1 php
2
3 define('ENVIRONMENT', 'development');
4
5 if (defined('ENVIRONMENT'))
6 {
7 switch (ENVIRONMENT)
8 {
9 case 'development':
10 error_reporting(E_ALL);
11 break;
12
13 case 'testing':
14 case 'production':
15 error_reporting(0);
16 break;
17
18 default:
19 exit('The application environment is not set correctly.');
20 }
21 }
22
23 /*
24 * SYSTEM FOLDER NAME
25 */
26 $system_path = 'system';
27
28 /*
29 * APPLICATION FOLDER NAME
30 */
31 $application_folder = 'application';
32
33 /*
34 * Resolve the system path for increased reliability
35 */
36 if (defined('STDIN'))
37 {
38 chdir(dirname(__FILE__));
39 }
40
41 if (realpath($system_path) !== FALSE)
42 {
43 $system_path = realpath($system_path).'/';
44 }
45
46 $system_path = rtrim($system_path, '/').'/';
47
48 if ( ! is_dir($system_path))
49 {
50 exit("xxxxxxxx");
51 }
52
53 /*
54 * set the main path constants
55 */
56 // The name of THIS file
57 define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
58
59 // this global constant is deprecataaed.
60 define('EXT', '.php');
61
62 // Path to the system folder
63 define('BASEPATH', str_replace("\", "/", $system_path));
64
65 // Path to the front controller (this file)
66 define('FCPATH', str_replace(SELF, '', __FILE__));
67
68 // Name of the "system folder"
69 define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));
70
71 // The path to the "application" folder
72 if (is_dir($application_folder))
73 {
74 define('APPPATH', $application_folder.'/');
75 }
76 else
77 {
78 if ( ! is_dir(BASEPATH.$application_folder.'/'))
79 {
80 exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);
81 }
82
83 define('APPPATH', BASEPATH.$application_folder.'/');
84 }
85
86 require_once BASEPATH.'core/CodeIgniter.php';