CI Framework Learning Note (ii)-Portal File index.php_php instance

Source: Internet
Author: User
Tags php cli rtrim switch case codeigniter
Previous section (CI Framework learning Note (i)-environment installation, basic terminology, and framework process) we mentioned the basic process of the CI framework, where we post the flowchart again 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 (! defined (' Environment ') OR! file_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, and stderr are the three constants that PHP defines as running in CLI (Command line Interface), and these three constants are similar to the stdin,stdout,stdout of the shell, PHP Standard input, standard output, and standard error streams in CLI mode. 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 _path)!== FALSE) {  $system _path = Realpath ($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. $application _folder. ' /');}

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):

<?phpdefine (' Environment ', ' development '), if (defined (' environment ')) {switch (Environment) {case ' development ':    Error_reporting (E_all);      Break    Case ' testing ': case ' production ': error_reporting (0);    Break  Default:exit (' The application environment is not set correctly. '); }}/* * SYSTEM FOLDER name */$system _path = ' SYSTEM ';/* * Application FOLDER name */$application _folder = ' application ';/* * Resolve the system path for increased reliability */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");} /* * Set the main path constants *///The name of this filedefine (' self ', pathinfo (__file__, Pathinfo_basename));//This G LOBAL constant is deprecataaed.define (' EXT ', '. php ');//Path to the System folderdefine (' BasePath ', str_replace ("\ \", "/" , $system _path));//path to the front COntroller (this file) define (' Fcpath ', str_replace (self, ", __file__));//Name of the" System Folder "define (' Sysdir ', tri M (STRRCHR (Trim (basepath, '/'), '/'), '/');//The path to the "Application" Folderif (Is_dir ($application _folder)) {Defin E (' 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 ';
  • Related Article

    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.