CI Framework source Reading notes 2 all the entrance index.php

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

In the previous section (CI Framework source reading note 1-environment preparation, basic terminology and framework process), we mentioned the basic process of the CI framework, this time to post a flowchart for reference:


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

1. Setting up the application environment
Define (' Environment ', ' development ');

Here the development can be any of your favorite environment name (such as Dev, and then test), you should in the following switch case code block, to set the environment to do related error control, otherwise, the CI framework will think you do not configure the appropriate environment, Thus exiting the process and giving the corresponding error message:

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

Why should I configure environment at the outset? This is because many of the components in the CI framework depend on the environment configuration, so let's take a look at where the system refers to environment:


As you can see, many components depend on environment. For example, to view system/config/common.php, which has a section of code that introduces a configuration file, is implemented like this:

if  definedfile_exists($file _path = APPPATH. ' config/'. Environment. ' /config.php ') {    $file _path = APPPATH. ' Config/config.php ';}

In the CI Framework, many of the configuration files are introduced in this way, so envrionment is necessary for the correct operation of the CI framework, so it is necessary to configure the environment at the beginning. One advantage of setting up environment is that it is convenient to switch the configuration of the system without having to modify the system code. For example, when the system enters the test phase, the database is configured as the test databases, and when the system is tested, the database switches to the online databases. This is like using a switch to control the environment of the system switch, natural is very convenient.

2. Configuring the System directory and application directory

  The CI framework allows you to separate the system core source code from the application, but you must set up the system folder and the application folder (again, the folder name can be any legitimate folder name, not necessarily the ' system ' and ' Application '):

$system _path = ' System '; $application _folder = ' application ';

Next, there is this piece of code:

 if   ( Defined  (' STDIN '  chdir  (dirname  (__file__  

What does this code do? First,STDIN,STDOUT, andSTDERR are the three constants that PHP defines as running in CLI (Command line Interface) mode. These three constants are similar to the Shell's stdin,stdout,stdout, which are standard input , standard output , and standard error streams in PHP CLI mode, respectively. That is, the three lines of code are to ensure that the CI framework works correctly in command-line mode. For more details on the PHP CLI, refer to: http://www.php-cli.com/

3. Validation of the system directory and application directory validation

(1). Verify the correctness of the system directory
Realpath returns the absolute directory name of the directory or file (no last/)


if (realpath($system _pathFALSE) {    $system _pathRealpath ($system _path). ' /';} $system _path RTrim ($system _path, '/'). ' /'; if Is_dir ($system _path)) {      exit("xxxxxxxx");} 

A few defined constants (the constant at the end of path indicates the directory path, and the variable at the end of Dir represents the directory name):
A. self (this refers to the index.php file)
B. EXT(deprecated, obsolete, no concern)
c. basepath(path to the system folder)
d. fcpath(Path of the front controller)
E. sysdir(System directory name)
F. APPPATH(application path)
to view the methods of all defined constants:

Print_r (get_defined_constants());

(2) application directory verification.

The code is simple and does not explain 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.. ' /'  

The last line of the portal file, introducing codeigniter.php(the key to the next reading). Codeigniter.php is called the bootstrap file, which is a bootstrap file that is the core 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, which provides a series of configuration parameters and correctness verification for the operation of CI framework, and these configurations and validations are the key to the normal operation of CI framework.

Finally, according to the Convention, paste the source code of the entire document (simplified annotated version):

1<?PHP2 3 Define(' Environment ', ' development ');4 5 if(defined(' Environment '))6 {7     Switch(Environment)8     {9          Case' Development ':Ten             error_reporting(E_all); One          Break; A      -          Case' Testing ': -          Case' Production ': the             error_reporting(0); -          Break; -  -         default: +             Exit(' The application environment is not set correctly. '); -     } + } A  at /* - * SYSTEM FOLDER NAME -  */ - $system _path= ' System '; -  - /* in * Application FOLDER NAME -  */ to $application _folder= ' Application '; +  - /* the * Resolve The system path for increased reliability *  */ $ if(defined(' STDIN '))Panax Notoginseng { -     chdir(dirname(__file__)); the } +  A if(Realpath($system _path) !==FALSE) the { +     $system _path=Realpath($system _path).‘ /‘; - } $  $ $system _path=RTrim($system _path, ‘/‘).‘ /‘; -  - if( !Is_dir($system _path)) the { -     Exit("xxxxxxxx");Wuyi } the  - /* Wu * Set the main path constants -  */ About //The name of this file $ Define(' Self ',PathInfo(__file__,pathinfo_basename)); -  - //This global constant is deprecataaed. - Define(' EXT ', '. php '); A  + //Path to the system folder the Define(' BasePath ',Str_replace("\\", "/",$system _path)); -  $ //Path to the front controller (this file) the Define(' Fcpath ',Str_replace(Self, ",__file__)); the  the //Name of the "System Folder" the Define(' Sysdir ',Trim(STRRCHR(Trim(BasePath, '/'), '/'), '/')); -  in //the path to the "Application" folder the if(Is_dir($application _folder)) the { About     Define(' APPPATH ',$application _folder.‘ /‘); the } the Else the { +     if( !Is_dir(BasePath.$application _folder.‘ /‘)) -     { the         Exit("Your Application Folder path does not appear to be set correctly. Please open the following file and correct this: ".Self );Bayi     } the  the     Define(' APPPATH ', basepath.$application _folder.‘ /‘); - } -  the require_onceBasePath. ' Core/codeigniter.php ';

CI Framework source Reading notes 2 all the entrance index.php

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: info-contact@alibabacloud.com 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.