CI framework Source Code Reading Notes 2 all entries index. php, ciindex. php_PHP tutorial

Source: Internet
Author: User
Tags php cli switch case
CI framework Source Code Reading Notes 2 all entries index. php, ciindex. php. CI framework Source Code Reading Notes 2 all entries index. php, ciindex. in the previous section of php (CI framework source code reading Note 1-environment preparation, basic terms, and framework process), we mentioned the CI framework source code reading Note 2 all entry indexes. php, ciindex. php

In the previous section (CI framework source code reading Note 1-environment preparation, basic terms, and framework processes), we mentioned the basic process of the CI framework. here we will paste 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? First,STDIN,STDOUT,STDERRIs PHP with CLI (Command Line Interface) Three constants defined for running in the php cli mode. these three constants are similar to the stdin, stdout, and stdout of the Shell.Standard input,Standard outputAndStandard error stream. 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(This 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.'/');}

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

1
 


Php ci framework CodeIgniter directory planning

If you need to use the framework, you need to put the first program in the project in the framework architecture, rather than creating an admin. php file in the root directory.
You should know that the CI framework's entry file is index. php, any page in it should be based on this entry file, that is, the access path is always index. php. php. in this case, the framework is useless if it is not accessed through the entry file.
Therefore, you should create an admin. php file in the controllers Directory of the application and use it according to the rules of the CI framework controller. in this way, the access path is index. php/admin.

Of course, you will assume that all URLs have an index. php is very ugly, so you can hide it through the routing rules of the CI framework, or you can use the pseudo static function of the server to hide it. But it is only hidden. the actual path still contains the index. php entry file.

How does the CI framework delete indexphp in the address bar?

1. modify Http. the LoadModule rewrite_module modules/mod_rewrite.so of conf removes comment 2. added the ci root directory. htaccess file RewriteEngine On RewriteBase/ci # Removes access to the system folder by users. # Additionally this will allow you to create a System. php controller, # previously this wocould not have been possible. # 'system' can be 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 system folder # This snippet prevents user access to the application folder # Submitted: fabdrol # Rename 'application' to your applications folder name. rewriteCond % {REQUEST_URI} ^ application. * RewriteRule ^ (. *) $/index. php? /$1 [L] # Checks to see if the user is attempting to access a valid file, # such as an image or css document, if this isn't true it sends the # request to index. php RewriteCond % {REQUEST_FILENAME }! -F RewriteCond % {REQUEST_FILENAME }! -D RewriteRule ^ (. *) $ index. php? /$1 [L] # If we don't have mod_rewrite installed, all 404's # can be sent to index. php, and everything works as normal. # Submitted by: ElliotHaughin ErrorDocument 404/index. php. htaccess file content

Index. php, ciindex. php in the previous section (CI framework source code reading Note 1-environment preparation, basic terms, and framework processes), we mentioned 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.