CodeIgniter import File parsing

Source: Internet
Author: User
Tags php cli switch case codeigniter
referred to the basic process of the CI framework, here again 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$application _folder .' /');} Else {    ifis_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.

The above describes the CodeIgniter import file parsing, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.