CI framework learning notes (2)-entry file index. php, _ PHP Tutorial

Source: Internet
Author: User
Tags php cli rtrim switch case
CI framework learning notes (2)-entry file index. php ,. CI framework learning notes (2)-entry file index. php, in the previous section (CI framework learning notes (I)-Environment installation, basic terms, and framework processes), we mentioned the basic CI framework learning notes for the CI framework (II) -index of the entry file. php,

In the previous section (CI framework learning notes (I)-Environment installation, basic terms, and framework processes), we mentioned the basic CI framework process. here we will re-post 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? STDIN, STDOUT, and STDERR are three constants defined by PHP to run in CLI (Command Line Interface) mode. these three constants are similar to Shell stdin, stdout, stdout, standard input, standard output, and standard error stream in php cli mode. 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 (here 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.'/');}

Introduce CodeIgniter. php in the last line of the entry file (which is also the key to 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 ):

<?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 global 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', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));// The path to the "application" folderif (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';




Upload (2)-index of the entry file. php, in the previous section (CI framework learning notes (I)-Environment installation, basic terms, and framework processes), we mentioned the basics of the CI framework...

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.