In the previous section (CI Framework source Reading note 1-environmental preparation, basic terminology, and framework process), we mentioned the basic process of the CI framework, and here again the flowchart is posted for reference:
As a CI frame entry file, source code reading, naturally starting from this. In the process of reading the source code, we do not explain line by row, but only the core function and implementation.
1. Setting up the application environment
Define ("Environment", "development");
The development can be any kind of environment name you like (Dev, test, for example), and you need to control the settings in the following switch case code block, otherwise the CI framework will assume that you are not configuring the appropriate environment. To exit the process and give the corresponding error message:
Default: exit ("The application environment is not set correctly.");
Why do I have to configure environment in the first? This is because many of the components in the CI framework depend on the configuration of the environment, and we look at the place in system where we refer to environment:
As you can see, many components are dependent on environment. For example, view the system/config/common.php, which includes a section of code that introduces a configuration file:
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, so envrionment is necessary for the proper operation of the CI framework, so you need to configure environment at the beginning. One advantage of setting up environment is that it is easy to switch the configuration of the system without having to modify the system code. For example, when the system goes into the test phase, the database is configured as a test, and when the system is tested, the database switches to the online databases. This is like using a switch to control the system's environmental switching, natural is very convenient.
2. Configure system directory and application directory
The CI framework allows you to separate the system core source code from the application codes, 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's this piece of code:
if (Defined ("STDIN"))
{
chdir (dirname (__file__));
}
What is this code for? First,STDIN,STDOUT, andSTDERR are three constants defined by PHP 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 the PHP CLI mode respectively. In other words, these three lines of code are designed 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. Correctness verification and application directory verification of the system directory
(1). Verification of 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");
}
Several defined constants (a constant at the end of path represents a directory path, and a variable at the end of Dir represents a directory name):
A. SELF(this refers to index.php file)
B. EXT(deprecated, obsolete, no concern)
C. basepath(path to the system folder)
D. Fcpath(the path to the front-end controller)
E. sysdir(System directory name)
F. AppPath(application path)
To view the methods for all defined constants:
Print_r (Get_defined_constants ());
(2) application directory validation.
The code is simpler and does not make too many explanations:
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 is 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(also the key to next reading). Codeigniter.php is known as bootstrap file, which is a boot file and a core document for the CI framework execution process.
Require_once BasePath. " Core/codeigniter.php ";
To sum up, index.php does not do much complex work, but resembles a logistics that provides a range of configuration parameters and correctness validation for the CI framework, which is key to the normal functioning of the CI framework.
Finally, follow the usual practice of pasting the entire file's source code (simplified annotation version):
<?php
define('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 file
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
// this global constant is deprecataaed.
define('EXT', '.php');
// Path to the system folder
define('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', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));
// The path to the "application" folder
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.'/');
}
require_once BASEPATH.'core/CodeIgniter.php';