ThinkPHP3.2 加载过程(二)
回顾:
上次介绍了 ThinkPHP 的 Index.PHP入口文件。但只是TP的入口前面的入口(刷boss总是要过好几关才能让你看到 ,不然boss都没面子啊),从Index.PHP最后一行把我们引路到了TP的大门前(ThinkPHP/ThinkPHP.php)
// 引入ThinkPHP入口文件require './ThinkPHP/ThinkPHP.php';
本次目标:
查看TP的大门,同时稍微探索一下大门内部的东西
正文:
先上代码
<span style="color: #008080;"> 1</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 记录开始运行时间</span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$GLOBALS</span>['_beginTime'] = <span style="color: #008080;">microtime</span>(<span style="color: #0000ff;">TRUE</span><span style="color: #000000;">);</span><span style="color: #008080;"> 3</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 记录内存初始使用</span><span style="color: #008080;"> 4</span> <span style="color: #008080;">define</span>('MEMORY_LIMIT_ON',<span style="color: #008080;">function_exists</span>('memory_get_usage'<span style="color: #000000;">));</span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">if</span>(MEMORY_LIMIT_ON) <span style="color: #800080;">$GLOBALS</span>['_startUseMems'] =<span style="color: #000000;"> memory_get_usage();</span><span style="color: #008080;"> 6</span> <span style="color: #008080;"> 7</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 版本信息</span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">const</span> THINK_VERSION = '3.2.3'<span style="color: #000000;">;</span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #008000;">//</span><span style="color: #008000;"> URL 模式定义</span><span style="color: #008080;">11</span> <span style="color: #0000ff;">const</span> URL_COMMON = 0; <span style="color: #008000;">//</span><span style="color: #008000;">普通模式</span><span style="color: #008080;">12</span> <span style="color: #0000ff;">const</span> URL_PATHINFO = 1; <span style="color: #008000;">//</span><span style="color: #008000;">PATHINFO模式</span><span style="color: #008080;">13</span> <span style="color: #0000ff;">const</span> URL_REWRITE = 2; <span style="color: #008000;">//</span><span style="color: #008000;">REWRITE模式</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">const</span> URL_COMPAT = 3; <span style="color: #008000;">//</span><span style="color: #008000;"> 兼容模式</span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #008000;">// 类文件后缀</span><span style="color: #008080;">17</span> <span style="color: #0000ff;">const</span> EXT = '.class.php'<span style="color: #000000;">; </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 系统常量定义</span><span style="color: #008080;">20</span> <span style="color: #008080;">defined</span>('THINK_PATH') or <span style="color: #008080;">define</span>('THINK_PATH', __DIR__.'/'<span style="color: #000000;">);</span><span style="color: #008080;">21</span> <span style="color: #008080;">defined</span>('APP_PATH') or <span style="color: #008080;">define</span>('APP_PATH', <span style="color: #008080;">dirname</span>(<span style="color: #800080;">$_SERVER</span>['SCRIPT_FILENAME']).'/'<span style="color: #000000;">);</span><span style="color: #008080;">22</span> <span style="color: #008080;">defined</span>('APP_STATUS') or <span style="color: #008080;">define</span>('APP_STATUS', ''); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用状态 加载对应的配置文件</span><span style="color: #008080;">23</span> <span style="color: #008080;">defined</span>('APP_DEBUG') or <span style="color: #008080;">define</span>('APP_DEBUG', <span style="color: #0000ff;">false</span>); <span style="color: #008000;">//</span><span style="color: #008000;"> 是否调试模式</span><span style="color: #008080;">24</span> <span style="color: #008080;">25</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">function_exists</span>('saeAutoLoader')){<span style="color: #008000;">//</span><span style="color: #008000;"> 自动识别SAE环境</span><span style="color: #008080;">26</span> <span style="color: #008080;">defined</span>('APP_MODE') or <span style="color: #008080;">define</span>('APP_MODE', 'sae'<span style="color: #000000;">);</span><span style="color: #008080;">27</span> <span style="color: #008080;">defined</span>('STORAGE_TYPE') or <span style="color: #008080;">define</span>('STORAGE_TYPE', 'Sae'<span style="color: #000000;">);</span><span style="color: #008080;">28</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{</span><span style="color: #008080;">29</span> <span style="color: #008080;">defined</span>('APP_MODE') or <span style="color: #008080;">define</span>('APP_MODE', 'common'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用模式 默认为普通模式 </span><span style="color: #008080;">30</span> <span style="color: #008080;">defined</span>('STORAGE_TYPE') or <span style="color: #008080;">define</span>('STORAGE_TYPE', 'File'); <span style="color: #008000;">//</span><span style="color: #008000;"> 存储类型 默认为File </span><span style="color: #008080;">31</span> <span style="color: #000000;">}</span><span style="color: #008080;">32</span> <span style="color: #008080;">33</span> <span style="color: #008080;">defined</span>('RUNTIME_PATH') or <span style="color: #008080;">define</span>('RUNTIME_PATH', APP_PATH.'Runtime/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 系统运行时目录</span><span style="color: #008080;">34</span> <span style="color: #008080;">defined</span>('LIB_PATH') or <span style="color: #008080;">define</span>('LIB_PATH', <span style="color: #008080;">realpath</span>(THINK_PATH.'Library').'/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 系统核心类库目录</span><span style="color: #008080;">35</span> <span style="color: #008080;">defined</span>('CORE_PATH') or <span style="color: #008080;">define</span>('CORE_PATH', LIB_PATH.'Think/'); <span style="color: #008000;">//</span><span style="color: #008000;"> Think类库目录</span><span style="color: #008080;">36</span> <span style="color: #008080;">defined</span>('BEHAVIOR_PATH')or <span style="color: #008080;">define</span>('BEHAVIOR_PATH', LIB_PATH.'Behavior/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 行为类库目录</span><span style="color: #008080;">37</span> <span style="color: #008080;">defined</span>('MODE_PATH') or <span style="color: #008080;">define</span>('MODE_PATH', THINK_PATH.'Mode/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 系统应用模式目录</span><span style="color: #008080;">38</span> <span style="color: #008080;">defined</span>('VENDOR_PATH') or <span style="color: #008080;">define</span>('VENDOR_PATH', LIB_PATH.'Vendor/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 第三方类库目录</span><span style="color: #008080;">39</span> <span style="color: #008080;">defined</span>('COMMON_PATH') or <span style="color: #008080;">define</span>('COMMON_PATH', APP_PATH.'Common/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用公共目录</span><span style="color: #008080;">40</span> <span style="color: #008080;">defined</span>('CONF_PATH') or <span style="color: #008080;">define</span>('CONF_PATH', COMMON_PATH.'Conf/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用配置目录</span><span style="color: #008080;">41</span> <span style="color: #008080;">defined</span>('LANG_PATH') or <span style="color: #008080;">define</span>('LANG_PATH', COMMON_PATH.'Lang/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用语言目录</span><span style="color: #008080;">42</span> <span style="color: #008080;">defined</span>('HTML_PATH') or <span style="color: #008080;">define</span>('HTML_PATH', APP_PATH.'Html/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用静态目录</span><span style="color: #008080;">43</span> <span style="color: #008080;">defined</span>('LOG_PATH') or <span style="color: #008080;">define</span>('LOG_PATH', RUNTIME_PATH.'Logs/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用日志目录</span><span style="color: #008080;">44</span> <span style="color: #008080;">defined</span>('TEMP_PATH') or <span style="color: #008080;">define</span>('TEMP_PATH', RUNTIME_PATH.'Temp/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用缓存目录</span><span style="color: #008080;">45</span> <span style="color: #008080;">defined</span>('DATA_PATH') or <span style="color: #008080;">define</span>('DATA_PATH', RUNTIME_PATH.'Data/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用数据目录</span><span style="color: #008080;">46</span> <span style="color: #008080;">defined</span>('CACHE_PATH') or <span style="color: #008080;">define</span>('CACHE_PATH', RUNTIME_PATH.'Cache/'); <span style="color: #008000;">//</span><span style="color: #008000;"> 应用模板缓存目录</span><span style="color: #008080;">47</span> <span style="color: #008080;">defined</span>('CONF_EXT') or <span style="color: #008080;">define</span>('CONF_EXT', '.php'); <span style="color: #008000;">//</span><span style="color: #008000;"> 配置文件后缀</span><span style="color: #008080;">48</span> <span style="color: #008080;">defined</span>('CONF_PARSE') or <span style="color: #008080;">define</span>('CONF_PARSE', ''); <span style="color: #008000;">//</span><span style="color: #008000;"> 配置文件解析方法</span><span style="color: #008080;">49</span> <span style="color: #008080;">defined</span>('ADDON_PATH') or <span style="color: #008080;">define</span>('ADDON_PATH', APP_PATH.'Addon'<span style="color: #000000;">);</span><span style="color: #008080;">50</span> <span style="color: #008080;">51</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 系统信息</span><span style="color: #008080;">52</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">version_compare</span>(<span style="color: #ff00ff;">PHP_VERSION</span>,'5.4.0','5c5a3685c3a670aea48a66f2eb72ba15 'm', // 默认模块获取变量<p> 'VAR_CONTROLLER' => 'c', // 默认控制器获取变量</p><p> 'VAR_ACTION' => 'a', // 默认操作获取变量</p><p> 值的定义在ThinkPHP\Conf\Convention.php</p><p> </p><div class="cnblogs_code"> <pre class="brush:php;toolbar:false"><span style="color: #008080;"> 1</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> 系统变量名称设置 </span><span style="color: #008000;">*/</span><span style="color: #008080;"> 2</span> 'VAR_MODULE' => 'm', <span style="color: #008000;">//</span><span style="color: #008000;"> 默认模块获取变量</span><span style="color: #008080;"> 3</span> 'VAR_ADDON' => 'addon', <span style="color: #008000;">//</span><span style="color: #008000;"> 默认的插件控制器命名空间变量</span><span style="color: #008080;"> 4</span> 'VAR_CONTROLLER' => 'c', <span style="color: #008000;">//</span><span style="color: #008000;"> 默认控制器获取变量</span><span style="color: #008080;"> 5</span> 'VAR_ACTION' => 'a', <span style="color: #008000;">//</span><span style="color: #008000;"> 默认操作获取变量</span><span style="color: #008080;"> 6</span> 'VAR_AJAX_SUBMIT' => 'ajax', <span style="color: #008000;">//</span><span style="color: #008000;"> 默认的AJAX提交变量</span><span style="color: #008080;"> 7</span> 'VAR_JSONP_HANDLER' => 'callback',<span style="color: #008080;"> 8</span> 'VAR_PATHINFO' => 's', <span style="color: #008000;">//</span><span style="color: #008000;"> 兼容模式PATHINFO获取变量例如 ?s=/module/action/id/1 后面的参数取决于URL_PATHINFO_DEPR</span><span style="color: #008080;"> 9</span> 'VAR_TEMPLATE' => 't', <span style="color: #008000;">//</span><span style="color: #008000;"> 默认模板切换变量</span><span style="color: #008080;">10</span> 'VAR_AUTO_STRING' => <span style="color: #0000ff;">false</span>, <span style="color: #008000;">//</span><span style="color: #008000;"> 输入变量是否自动强制转换为字符串 如果开启则数组变量需要手动传入变量修饰符获取变量</span>
PATHINFO模式
1 const URL_PATHINFO = 1; //PATHINFO模式 该模式是默认模式
URL: http://localhost/index.php/home/user/login/var/value/
PATHINFO地址的前三个参数分别表示模块/控制器/操作。
说白了就是把普通模式下的几个M C A 变量给省掉 使用斜杠来代替
好处:1.看上去很整齐 2.据说可以提高被搜索引擎抓住的几率哦!!!
当然是斜杠还是别的是可以设置的在 ThinkPHP\Conf\Convention.php 文件中可以进行设置
1 'URL_PATHINFO_DEPR' => '/', // PATHINFO模式下,各参数之间的分割符号
REWRITE模式
1 const URL_REWRITE = 2; //REWRITE模式
官方的原话 : REWRITE模式是在PATHINFO模式的基础上添加了重写规则的支持,可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则。
如何是这样,那么我觉得应该就是 浏览器地址给重写以后在按照PATHINFO模式进行解析路由
兼容模式
1 const URL_COMPAT = 3; // 兼容模式
URL http://localhost/?s=/home/user/login/var/value
看到这个地址 YII的人有木有感觉很熟悉
为什么是S开头呢?是因为配置文件的设置 在 ThinkPHP\Conf\Convention.php
1 'VAR_PATHINFO' => 's', // 兼容模式PATHINFO获取变量例如 ?s=/module/action/id/1 后面的参数取决于URL_PATHINFO_DEPR
修改模式方法
在配置文件 ThinkPHP\Conf\Convention.php
1 'URL_MODEL' => 1, // URL访问模式,可选参数0、1、2、3,代表以下四种模式: 2 // 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE 模式); 3 (兼容模式) 默认为PATHINFO 模式
对于以上集中模式个人的小总结:
不管是那个 我们把URL中出了模块名称开始前都去掉,就是不同的几种取出模块、控制器、控制器其中的方法名。所以在不配置路由和不使用默认的情况下,我们都能看到这三个参数。
总结:
ThinkPHP\ThinkPHP.php文件中 最主要的目的还是定义好各种目录的变量方便以后的调用
如果你发现代码中有一个定义你不知道做什么,如果是_path结尾(关于目录的),就去ThinkPHP\ThinkPHP.php中找找,别的那么就去ThinkPHP\Conf\Convention.php下找找 基本都能找到
几个思考(个人在整理时候想到的):
1.定义IS_CGI ,IS_WIN,IS_CLI,MAGIC_QUOTES_GPC干嘛用
<span style="color: #008080;"> 1</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 系统信息</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">if</span>(<span style="color: #008080;">version_compare</span>(<span style="color: #ff00ff;">PHP_VERSION</span>,'5.4.0','<'<span style="color: #000000;">)) {</span><span style="color: #008080;"> 3</span> <span style="color: #008080;">ini_set</span>('magic_quotes_runtime',0<span style="color: #000000;">);</span><span style="color: #008080;"> 4</span> <span style="color: #008080;">define</span>('MAGIC_QUOTES_GPC',<span style="color: #008080;">get_magic_quotes_gpc</span>()? <span style="color: #0000ff;">true</span> : <span style="color: #0000ff;">false</span><span style="color: #000000;">);</span><span style="color: #008080;"> 5</span> }<span style="color: #0000ff;">else</span><span style="color: #000000;">{</span><span style="color: #008080;"> 6</span> <span style="color: #008080;">define</span>('MAGIC_QUOTES_GPC',<span style="color: #0000ff;">false</span><span style="color: #000000;">);</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 8</span> <span style="color: #008080;">define</span>('IS_CGI',(0 === <span style="color: #008080;">strpos</span>(PHP_SAPI,'cgi') || <span style="color: #0000ff;">false</span> !== <span style="color: #008080;">strpos</span>(PHP_SAPI,'fcgi')) ? 1 : 0<span style="color: #000000;"> );</span><span style="color: #008080;"> 9</span> <span style="color: #008080;">define</span>('IS_WIN',<span style="color: #008080;">strstr</span>(<span style="color: #ff00ff;">PHP_OS</span>, 'WIN') ? 1 : 0<span style="color: #000000;"> );</span><span style="color: #008080;">10</span> <span style="color: #008080;">define</span>('IS_CLI',PHP_SAPI=='cli'? 1 : 0);
2.为什么我们刚下载的架构不需要输入模块、控制器、方法名也能正常跳出页面。
好了,下一次我们就拿上小米加步枪杀进TP大门
1 // 应用初始化 2 Think\Think::start();