CI framework source code reading notes 2 All entrances index.php, ciindex.php_PHP tutorial

WBOY
Release: 2016-07-13 10:16:01
Original
909 people have browsed it

CI framework source code reading notes 2 All entrances index.php, ciindex.php

Previous section (CI framework source code reading notes 1 - environment preparation, basic terminology and framework process), we mentioned the basic process of the CI framework. The flow chart is posted here again for reference:


As the entry file of the CI framework, source code reading naturally starts here. In the process of reading the source code, we will not explain it line by line, but only the core functions and implementation.

1. Set up the application environment

<span>define</span>('ENVIRONMENT', 'development');
Copy after login

The development here can be any environment name you like (such as dev, or test). Correspondingly, you have to make relevant errors for the set environment in the switch case code block below. Control, otherwise, the CI framework will think that you have not configured the corresponding environment, thus exiting the process and giving the corresponding error message:

<span>default</span>:     <span>exit</span>('The application environment is not set correctly.');
Copy after login

Why do you need to configure ENVIRONMENT in the first place? This is because many components in the CI framework depend on the configuration of ENVIRONMENT. Let’s take a look at the places where ENVIRONMENT is referenced in the system:


You can see that many components depend on ENVIRONMENT. For example, look at system/config/Common.php. There is a code that introduces the configuration file, which is implemented like this:

<span>if</span> ( ! <span>defined</span>('ENVIRONMENT') OR ! <span>file_exists</span>(<span>$file_path</span> = APPPATH.'config/'.ENVIRONMENT.'/config.php'<span>))
{
    </span><span>$file_path</span> = APPPATH.'config/config.php'<span>;
}</span>
Copy after login

In the CI framework, many configuration files are introduced in this way, so ENVIRONMENT is necessary for the correct operation of the CI framework, so ENVIRONMENT needs to be configured at the beginning. One benefit of setting ENVIRONMENT is that you can easily switch the system configuration without modifying the system code. For example, when the system enters the test phase, the database is configured as the test database, and when the system test is completed, the database is switched to the online database. This is like using a switch to control the environment switching of the system, which is naturally very convenient.

2. Configure the system directory and application directory

 The CI framework allows you to separate the system core source code and application code, but you must set up the system's system folder and application folder (similarly, the folder name can be Any legal folder name, not necessarily 'system' and 'application'):

<span>$system_path</span> = 'system'<span>;
</span><span>$application_folder</span> = 'application';
Copy after login

Next, there is this piece of code:

<span>if</span> (<span>defined</span>('STDIN'<span>))
{
     </span><span>chdir</span>(<span>dirname</span>(<span>__FILE__</span><span>));
}</span>
Copy after login

What is this code for? First, STDIN, STDOUT, STDERR is PHP’s CLI (Command Line Interface) three constants defined for running in mode. These three constants are similar to Shell’s stdin, stdout, stdout, which are respectively in PHP CLI mode. Standard input , standard output and standard error stream. In other words, these three lines of code are to ensure that the CI framework can run normally in command line mode. For more details about PHP CLI, please refer to: http://www.php-cli.com/

3. Verification of the correctness of the system directory and application directory

(1). The correctness verification of system directory
Realpath returns the absolute directory name of the directory or file (without the final /)

<span>if</span> (<span>realpath</span>(<span>$system_path</span>) !== <span>FALSE</span><span>)
{
    </span><span>$system_path</span> = <span>realpath</span>(<span>$system_path</span>).'/'<span>;
}
</span><span>$system_path</span> = <span>rtrim</span>(<span>$system_path</span>, '/').'/'<span>;
</span><span>if</span> ( ! <span>is_dir</span>(<span>$system_path</span><span>))
{  
    </span><span>exit</span>("xxxxxxxx"<span>);
}</span>
Copy after login

Several defined constants (the constant at the end of PATH represents the directory path, and the variable at the end of DIR represents the directory name):
a.   SELF (here refers to the index.php file)
b. EXT(deprecated, abandoned, no need to pay attention)
c. BASEPATH(path to the system folder)
d. FCPATH( The path of the front-end controller)
e.                                                                                                                                        Path. APPPATH(application path)
Method to view all defined constants:
(2) Directory verification of application.

<span>Print_r</span>(<span>get_defined_constants</span>());
Copy after login

The code is relatively simple, no need to explain too much:

In the last line of the entry file, introduce

CodeIgniter.php (also the key to the next step of reading). CodeIgniter.php is called bootstrap file, which means it is a bootstrap file and is the core file of the CI framework execution process.

<span>if</span> (<span>is_dir</span>(<span>$application_folder</span><span>))
{
    </span><span>define</span>('APPPATH', <span>$application_folder</span>.'/'<span>);
}
</span><span>else</span><span>
{
    </span><span>if</span> ( ! <span>is_dir</span>(BASEPATH.<span>$application_folder</span>.'/'<span>))
    {
        </span><span>exit</span>("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".<span>SELF);
    }

    </span><span>define</span>('APPPATH', BASEPATH.<span>$application_folder</span>.'/'<span>);
}</span>
Copy after login
<span>require_once</span> BASEPATH.'core/CodeIgniter.php';
Copy after login

  总结一下,index.php并没有做太多复杂的工作,而是类似一个后勤,为CI框架的运行提供了一系列配置参数和正确性验证,而这些配置和验证,是CI框架能够正常运行的关键。

  最后,按照惯例,贴一下整个文件的源码(简化注释版):

 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 <span>require_once</span> BASEPATH.'core/CodeIgniter.php';
Copy after login

 

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文件内容
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/900269.htmlTechArticleCI框架源码阅读笔记2 一切的入口 index.php,ciindex.php 上一节(CI框架源码阅读笔记1 - 环境准备、基本术语和框架流程)中,我们提到了CI框架...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template