Home  >  Article  >  Backend Development  >  CodeIgniter entry file analysis

CodeIgniter entry file analysis

WBOY
WBOYOriginal
2016-07-29 09:14:551290browse
Mentioned the basic process of the CI framework, and here is the flow chart again for reference:

CodeIgniter entry file analysis

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

1. Set up the application environment

<span>define</span><span>('ENVIRONMENT', 'development');</span>

The development here can be any environment name you like (such as dev, and then test). Correspondingly, you need to use the switch case code block below , perform relevant error control on the set environment, otherwise, the CI framework will think that you have not configured the corresponding environment, thus exiting the process and giving the corresponding error message:

<span>default</span><span>:     <span>exit</span>('The application environment is not set correctly.');</span>

Why do you need to do this in the first place? Configure ENVIRONMENT? This is because many components in the CI framework depend on the configuration of ENVIRONMENT. Let's take a look at the places where ENVIRONMENT is referenced in system:

CodeIgniter entry file analysis
 You can see that many components depend on ENVIRONMENT. For example, view system/config /Common.php, there is a piece of code that introduces configuration files, which is implemented like this:

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

In the CI framework, many configuration files are introduced in this way, so ENVRIONMENT is very important to the CI framework. It is necessary for correct operation, so ENVIRONMENT needs to be configured at the beginning. One benefit of setting ENVIRONMENT is that you can easily switch the system configuration without modifying the system code. For example, when the system enters the test phase, the database is configured as the test database, and when the system test is completed, the database is switched to the online database. This is like using a switch to control the environment switching of the system, 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 and application code, but you must set up the system system folder and application folder ( Similarly, the folder name can be any legal folder name, without necessarily using 'system' and 'application'):

<span>$system_path</span><span> = 'system'<span>;
</span><span>$application_folder</span> = 'application';</span>

Next, there is such a piece of code:

<span>if</span><span> (<span>defined</span>('STDIN'<span>))
{
     </span><span>chdir</span>(<span>dirname</span>(<span>__FILE__</span><span>));
}</span></span>

This What is the code snippet for? First of all, STDIN, STDOUT, STDERR are PHP CLI (Command Line Interface) mode operation defined three constants, these three constants are similar to Shell's stdin, stdout, stdout, respectively standard input, standard output and standard error stream in PHP CLI mode. In other words, these three lines of code are to ensure that the CI framework can run normally in command line mode. About PHP For more details on CLI, please refer to: http://www.php-cli.com/

3. Verification of the correctness of the system directory and verification of the application directory

(1). Verification of the correctness of the system directory
Realpath returns the absolute directory name of the directory or file (without the final /)

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

Several defined constants (the constant at the end of PATH represents the directory path, and the variable at the end of DIR represents the directory name):
a. SELF (here refers to the index.php file)
b. EXT(deprecated, abandoned, no need to pay attention)
c. BASEPATH (path to the system folder)
d. FCPATH (path to front-end controller)
e. SYSDIR (system directory name)
f. APPPATH (application path)
View all definitions constant method :

<span>Print_r</span><span>(<span>get_defined_constants</span>());</span>

CodeIgniter entry file analysis

(2) Directory verification of application.

The code is relatively simple and does not require too much explanation:

<span>if</span><span> (<span>is_dir</span>(<span>$application_folder</span><span>))
{
    </span><span>define</span>('APPPATH', <span>$application_folder</span>.'/'<span>);
}
</span><span>else</span><span>
{
    </span><span>if</span> ( ! <span>is_dir</span>(BASEPATH.<span>$application_folder</span>.'/'<span>))
    {
        </span><span>exit</span>("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".<span>SELF);
    }

    </span><span>define</span>('APPPATH', BASEPATH.<span>$application_folder</span>.'/'<span>);
}</span></span>

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

<span><strong>require</strong>_once</span><span> BASEPATH.'core/CodeIgniter.php';</span>

 To summarize, index.php does not do too much complicated work, but is like a logistics, providing a series of configuration parameters and correctness verification for the operation of the CI framework, and these configurations and verifications are The key to the normal operation of the CI framework.

The above introduces the analysis of CodeIgniter entry file, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn