CI Framework source Reading notes 2 All the entrance index.php,ciindex.php_php tutorials

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

CI Framework source Reading notes 2 all the entrance index.php,ciindex.php


In the previous section (CI Framework source reading note 1-environment preparation, basic terminology, and framework process), we mentioned the basic process of the CI framework, where we post the flowchart again 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 defined file_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.

Finally, according to the Convention, paste the source code of the entire document (simplified annotated version):

1 
 Php2 3 Define(' Environment ', ' development ');4 5 if(defined(' Environment '))6 {7     Switch(Environment)8     {9          Case' Development ':Ten             error_reporting(E_all); One          Break; A      -          Case' Testing ': -          Case' Production ': the             error_reporting(0); -          Break; -  -         default: +             Exit(' The application environment is not set correctly. '); -     } + } A  at /* - * SYSTEM FOLDER NAME -  */ - $system _path= ' System '; -  - /* in * Application FOLDER NAME -  */ to $application _folder= ' Application '; +  - /* the * Resolve The system path for increased reliability *  */ $ if(defined(' STDIN '))Panax Notoginseng { -     chdir(dirname(__file__)); the } +  A if(Realpath($system _path) !==FALSE) the { +     $system _path=Realpath($system _path).' /'; - } $  $ $system _path=RTrim($system _path, '/').' /'; -  - if( !Is_dir($system _path)) the { -     Exit("xxxxxxxx");Wuyi } the  - /* Wu * Set the main path constants -  */ About //The name of this file $ Define(' Self ',PathInfo(__file__,pathinfo_basename)); -  - //This global constant is deprecataaed. - Define(' EXT ', '. php '); A  + //Path to the system folder the Define(' BasePath ',Str_replace("\\", "/",$system _path)); -  $ //Path to the front controller (this file) the Define(' Fcpath ',Str_replace(Self, ",__file__)); the  the //Name of the "System Folder" the Define(' Sysdir ',Trim(STRRCHR(Trim(BasePath, '/'), '/'), '/')); -  in //the path to the "Application" folder the if(Is_dir($application _folder)) the { About     Define(' APPPATH ',$application _folder.' /'); the } the Else the { +     if( !Is_dir(BasePath.$application _folder.' /')) -     { the         Exit("Your Application Folder path does not appear to be set correctly. Please open the following file and correct this: ".Self );Bayi     } the  the     Define(' APPPATH ', basepath.$application _folder.' /'); - } -  the require_onceBasePath. ' core/codeigniter.php ';


The planning problem of the CodeIgniter directory for the CI framework of PHP

You need to use the framework, then you have to put the first program in the project in the framework schema, and not in the root directory to create a new admin.php.
You should be aware that the CI framework entry file is index.php, and any page in it should be based on this portal file, that is, the access path is always the index.php/***** form, Instead of a single admin.php, so that, without access to the portal file, the framework's utility is gone.
Therefore, you should build a admin.php in the Controllers directory in application and use it according to the rules of the CI framework controller so that the access path is index.php/admin.

Of course, you would think that all URLs have a index.php that is ugly, so you can hide them through the CI framework's routing rules, or you can use the pseudo-static functionality of the server. But it is only hidden, the actual path still has index.php this entry file.

How does the CI framework remove the indexphp of the address bar

1. Modify the http.conf loadmodule rewrite_module modules/mod_rewrite.so Remove the comment 2.ci root directory Add. htaccess file Rewriteengine on Rewritebase /ci #Removes access to the system, folder by users. #Additionally this would allow you to create a system.php controller, #previously this would not having been possible. # ' system ' can is replaced if you have renamed your system folder. Rewritecond%{request_uri} ^system.* rewriterule ^ (. *) $/index.php?/$1 [L] #When your application folder isn ' t in the Syst Em folder #This Snippet prevents user access to the application Folder #Submitted By:fabdrol #Rename ' application ' to you R Applications folder name. Rewritecond%{request_uri} ^application.* rewriterule ^ (. *) $/index.php?/$1 [L] #Checks to see if the user is attempting T o Access a valid file, #such as an image or CSS document, if this isn ' t true it sends the #request to index.php Rewritecon D%{request_filename}!-f rewritecond%{request_filename}!-d rewriterule ^ (. *) $ index.php?/$1 [L] # If We don ' t have mod_ Rewrite Installed, all 404 ' s # can is sent to index.php, and everything works as normal. # submitted By:elliothaughin errordocument 404/index.php. htaccess file Contents

http://www.bkjia.com/PHPjc/900269.html www.bkjia.com true http://www.bkjia.com/PHPjc/900269.html techarticle CI Framework Source reading Note 2 All the entrance to the index.php,ciindex.php section (CI framework source reading note 1-environment preparation, basic terminology and framework process), we mention 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.