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

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

CI framework Source Code Reading Notes 2 all entries index. 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: http://www.php-cli.com/

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:

Print_r(get_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 
 

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.