CI framework Source Code Reading Notes 2 all entries index. php, ciindex. php

Source: Internet
Author: User
Tags php cli switch case codeigniter

CI framework Source Code Reading Notes 2 all entries index. php, ciindex. php

In the previous section (CI framework Source Code Reading Note 1-Environment preparation, basic terms, and Framework processes), we mentioned the basic process of the CI framework. Here we will paste a flowchart for your reference:

As the entry file of the CI framework, the source code is read, and thus it naturally begins. In the process of reading the source code, we will not explain it line by line, but will only explain the core functions and implementations.

1. Set the application environment
define('ENVIRONMENT', 'development');

The development here can be the name of any environment you like (such as dev and test). Correspondingly, you need to go to the switch case code block below, perform related error control on the set environment. Otherwise, the CI framework will think that you have not configured the corresponding environment to exit the process and give the corresponding error message:

default:     exit('The application environment is not set correctly.');

Why ENVIRONMENT should be configured at the beginning? This is because many components in the CI framework depend on the ENVIRONMENT configuration. Let's take a look at the reference of ENVIRONMENT in system:

As you can see, many components depend on ENVIRONMENT. For example, you can view system/config/Common. php. Here is a piece of code that introduces the configuration file, which is implemented as follows:

if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php')){    $file_path = APPPATH.'config/config.php';}

In the CI framework, many configuration files are introduced in this way. Therefore, ENVRIONMENT is required when the CI framework is correctly run. Therefore, ENVIRONMENT must be configured at the beginning. One advantage of setting ENVIRONMENT is that you can easily switch system configurations without having to modify system code. For example, when the system enters the test stage, the database is configured as the tested database, and when the system test is complete, the database is switched to the online database. This is like using a switch to control the system environment switch, 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 from the application code, but you must set the system folder and the application folder (similarly, the folder name can be any legal folder name, instead of using 'system' and 'application '):

$system_path = 'system';$application_folder = 'application';

Next, there is such a piece of code:

if (defined('STDIN')){     chdir(dirname(__FILE__));}

What is this code? First,STDIN,STDOUT,STDERRIs PHP with CLI (Command Line Interface) Three constants defined for running in the php cli mode. These three constants are similar to the stdin, stdout, and stdout of the Shell.Standard Input,Standard outputAndStandard Error stream. That is to say, the three lines of code are used to ensure that the CI framework can run normally in command line mode. For more details about php cli, refer to:

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

(1). verify the correctness of the system directory
Realpath returns the absolute Directory Name of the directory or file (No final /)

if (realpath($system_path) !== FALSE){    $system_path = realpath($system_path).'/';}$system_path = rtrim($system_path, '/').'/';if ( ! is_dir($system_path)){      exit("xxxxxxxx");}

Several defined constants (constants at the end of PATH represent the directory PATH, and variables at the end of DIR represent the directory name ):
A.SELF(This refers to the index. php file)
B.EXT(Deprecated, obsolete, do not pay attention)
C.BASEPATH(Path of the system folder)
D.FCPATH(Path of the front-end Controller)
E.SYSDIR(System directory name)
F.APPPATH(Application Path)
Methods for viewing all defined constants:


(2) verify the application directory.

The code is relatively simple and does not explain it too much:

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.'/');}

The last line of the entry file.CodeIgniter. php(It is also the key to the next reading ). CodeIgniter. php is called bootstrap file, which is a boot file and the core file of the CI framework execution process.

require_once BASEPATH.'core/CodeIgniter.php';

To sum up, index. php does not do much complicated work, but is similar to a logistics. It provides a series of configuration parameters and correctness verification for the CI framework, it is the key to the normal operation of the CI framework.

Finally, paste the source code of the entire file (simplified comment version) as usual ):

 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 NAME25  */26 $system_path = 'system';27 28 /*29  * APPLICATION FOLDER NAME30  */31 $application_folder = 'application';32 33 /*34  *  Resolve the system path for increased reliability35  */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 constants55  */56 // The name of THIS file57 define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));58 59 // this global constant is deprecataaed.60 define('EXT', '.php');61 62 // Path to the system folder63 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" folder72 if (is_dir($application_folder))73 {74     define('APPPATH', $application_folder.'/');75 }76 else77 {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';


Php ci framework CodeIgniter directory Planning

If you need to use the framework, you need to put the first program in the project in the framework architecture, rather than creating an admin. php file in the root directory.
You should know that the CI Framework's entry file is index. php, any page in it should be based on this entry file, that is, the access path is always index. php. php. In this case, the framework is useless if it is not accessed through the entry file.
Therefore, you should create an admin. php file in the controllers directory of the application and use it according to the rules of the CI framework controller. In this way, the access path is index. php/admin.

Of course, you will assume that all URLs have an index. php is very ugly, so you can hide it through the routing rules of the CI framework, or you can use the pseudo static function of the server to hide it. But it is only hidden. The actual path still contains the index. php entry file.

How does the CI framework Delete indexphp in the address bar?

1. modify Http. the LoadModule rewrite_module modules/ of conf removes comment 2. added the ci root directory. htaccess file 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 wocould 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: 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 File Content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.