thinkphp Source Analysis Series (i) – entry file

Source: Internet
Author: User
Tags php framework rtrim

As described in the official documentation, thinkphp uses a single portal, and all requests are entered from the default index.php file. Of course not that must be entered from the index.php, this should depend on your server configuration, the general server will have a default home page, such as index.php,index.html, so the general access to the domain name will be the default access to the above file, you can also create multiple applications, One application corresponds to a portal file, and all portal files refer to a set of thinkphp class Libraries.

Let's see what index.php has done. index.php The primary task is to define the app name and reference the class library path. of course, You can also define some other system Constants.

 //  detect PHP environment   If  (version_compare  (php_version , ' 5.3.0 ', ' < ') die  (' require PHP > 5.3.0! ')  //  Open debug mode suggest the development phase to open the deployment phase comment or set to False  define  (' app_debug ', true   //  Define the application directory  define  (' app_path ', './test/'  //  Introducing thinkphp Portal file  require  './thinkphp/thinkphp.php '; 

Thinkphp official recommendation in the actual deployment of the application, we recommend that in addition to the application portal files and Public resource directory, Other files are placed under the Non-web directory, with better Security.

Next should be the thinkphp.php file, which is mainly used for some settings of the framework environment, including common paths, version detection, system Information and so On.

<?PHP//record start run time$GLOBALS[' _begintime '] =Microtime(TRUE);//Recording Initial memory usageDefine(' memory_limit_on ',function_exists(' Memory_get_usage '));if(memory_limit_on)$GLOBALS[' _startusemems '] =memory_get_usage ();//Version InformationConstthink_version = ' 3.2.2 '; //URL Pattern DefinitionConstUrl_common = 0;//Normal ModeConstUrl_pathinfo = 1;//pathinfo ModeConstUrl_rewrite = 2;//Rewrite modeConstUrl_compat = 3;//compatibility mode//class file suffixConstEXT = '. class.php '; //System Constants Define//define The directory where the current portal file resides, that is, the directory of the thinkphp frameworkdefined(' Think_path ') orDefine(' Think_path ', __dir__. ') /‘);//defines the directory where the site app Resides. In general, we will define the application path in the index.php file, and if we do not define App_path in the index.php file, then the Define (' app_path ', dirname ($_server[') will be Executed. Script_filename ']). ' /'); this gets the absolute path of the server side of the current execution script. This is typically the directory where the index.php file Resides. On my computer is d:/phpstudy/www/thinkphp/, which is the root directory of the Web Site. defined(' App_path ') orDefine(' App_path ',dirname($_server[' script_filename ']). ' /‘);defined(' App_status ') orDefine(' app_status ', ');//apply state to load the corresponding configuration filedefined(' App_debug ') orDefine(' App_debug ',false);//whether to debug mode /*this defines the application Pattern. What exactly is an application pattern? The thinkphp framework provides developers with the opportunity to change the core Framework. We know that the core of a PHP framework is to define some important configuration files, introduce some important class libraries and functions, and adapt the PHP configuration to the current host Environment. So we can separate the file codes that need to be introduced in these cores, all in a PHP configuration file, which is called the configuration file of the Schema. Then we refer to the corresponding configuration file according to the pattern, analyze the configuration item in the configuration file, and run it, so as to achieve the purpose of changing the framework Core. Use the most common pattern to Illustrate. We define the App_mode as common in the portal file, and then when we execute the Thinkphp's start method, We parse the defined schema name and go down to the mode folder according to the schema name to find the corresponding common.php File,//read the application Mode $ mode = include Is_file (conf_path. ') core.php ')? Conf_path. ' core.php ': Mode_path. App_mode. '. PHP '; This file defines the configuration items for extending the core, and after the introduction of this file, we can look at the start method followed by the introduction of the individual files defined in the configuration Item. At the same time, TP supports the SAE environment by default, which means that the official has written us the code for the core framework extension of the SAE environment, triggering only when Appropriate. */if(function_exists(' Saeautoloader ')) {//Automatic identification of the SAE environmentdefined(' App_mode ') orDefine(' app_mode ', ' SAE ');defined(' Storage_type ') orDefine(' storage_type ', ' Sae ');}Else{defined(' App_mode ') orDefine(' app_mode ', ' common ');//app mode defaults to normal modedefined(' Storage_type ') orDefine(' storage_type ', ' File ');//the storage type defaults to file} defined(' Runtime_path ') orDefine(' Runtime_path ', App_path. ') runtime/');//System Run-time Directorydefined(' Lib_path ') orDefine(' Lib_path ',Realpath(think_path. ' Library '). ' /‘);//system core Class Library directorydefined(' Core_path ') orDefine(' Core_path ', Lib_path. ') think/');//Think class Library directorydefined(' Behavior_path ') orDefine(' Behavior_path ', Lib_path. ') behavior/');//Behavior Class Library Directorydefined(' Mode_path ') orDefine(' Mode_path ', Think_path. ') mode/');//System Application Mode directorydefined(' Vendor_path ') orDefine(' Vendor_path ', Lib_path. ') vendor/');//Third-party class Library catalogsdefined(' Common_path ') orDefine(' Common_path ', App_path. ') common/');//Apply Public Directorydefined(' Conf_path ') orDefine(' Conf_path ', Common_path. ') conf/');//Application Configuration Directorydefined(' Lang_path ') orDefine(' Lang_path ', Common_path. ') lang/');//Application Language Directorydefined(' Html_path ') orDefine(' Html_path ', App_path. ') html/');//apply a static directorydefined(' Log_path ') orDefine(' Log_path ', Runtime_path. ') logs/');//Application Log Directorydefined(' Temp_path ') orDefine(' Temp_path ', Runtime_path. ') temp/');//Apply Cache Directorydefined(' Data_path ') orDefine(' data_path ', Runtime_path. ') data/');//Application Data Catalogdefined(' Cache_path ') orDefine(' Cache_path ', Runtime_path. ') cache/');//Apply Template Cache directorydefined(' Conf_ext ') orDefine(' conf_ext ', '. php ');//configuration file suffixdefined(' Conf_parse ') orDefine(' Conf_parse ', ');//configuration file resolution Method//system information/*in the case of magic_quotes_gpc=on, characters such as single quotation marks ('), double quotation marks ("), backslashes () and NUL (NULL Characters) are added as backslashes if the input data is Entered. These escapes are necessary, and if this option is off, then we must call the Addslashes function to add escape to the String. This feature was abolished after the php5.4. So we will not rely on this feature in the Future. In order to make your own program regardless of the server is what settings can be performed normally. You can use Get_magic_quotes_runtime to detect the state of the setting at the beginning of the program to determine whether you want to handle it manually, or to turn off the setting with Set_magic_quotes_runtime (0) when you start (or do not need automatic escaping). Determine the PHP version, less than 5.4 manual switch off, define Constants. A value greater than 5.4 directly defines the constant to False. */if(Version_compare(php_version, ' 5.4.0 ', ' < ')) {Ini_set(' magic_quotes_runtime ', 0); Define(' MAGIC_QUOTES_GPC ',GET_MAGIC_QUOTES_GPC()?True:False);}Else{Define(' MAGIC_QUOTES_GPC ',false);}/*PHP Judgment parsing PHP service is by the kind of server software, is the use of that protocol, Php_aspi is a direct use of the Constant. If it is a nginx+fastcgi environment, then its value is cgi-fcgi if it is the Apache environment, then his value is apache2handler, if it is the form of command line, then its value is Cliphp_os PHP is located in the name of the operating system, such as Linux and Win. Fully understand the various operating modes of php, see: http://www.jb51.net/article/37756.htmhttp://www.cnblogs.com/liuzhang/p/3929198.html*/Define(' is_cgi ', (0 = = =Strpos(php_sapi, ' cgi ') | |false!==Strpos(php_sapi, ' fcgi ')) ? 1:0 );Define(' Is_win ',strstr(Php_os, ' WIN ')? 1:0 );Define(' is_cli ', php_sapi== ' CLI '? 1:0); /*Specifies the file name of the currently running script, if it is not a command-line mode. */if(!Is_cli) {//Current file nameif(!defined(' _php_file_ ')) {if(is_cgi) {//cgi/fastcgi Mode$_temp=Explode('. PHP ',$_server[' php_self ']);Define(' _php_file_ ',RTrim(Str_replace($_server[' http_host '], ',$_temp[0]. '. PHP '), '/'));}Else {Define(' _php_file_ ',RTrim($_server[' Script_name '], '/'));}}if(!defined(' __root__ ')) {$_root=RTrim(dirname(_php_file_), '/');Define(' __root__ ', ($_root= = '/' | |$_root= = ' \ \ ')? ':$_root)); }} //load core Think classrequireCore_path. ' Think '.EXT;//Application InitializationThink\think::start ();

thinkphp Source Analysis Series (i) – entry file

Related Article

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.