ThinkPHP5 0原始碼學習之框架啟動流程

2022-03-18 01:02:29 字數 2573 閱讀 3859

thinkphp5框架的啟動流程圖如下:

thinkphp5的啟動流程按照檔案分為三步:

1、請求入口(public/index.php)

2、框架啟動(thinkphp/start.php)

thinkphp框架預設是單入口框架,預設的入口檔案為public/index.php,所有請求全部經過入口檔案index.php來進行訪問。

index.php**如下:

// 定義應用目錄

// 載入框架引導檔案

require __dir__ . '/../thinkphp/start.php';

做了兩件事:

2、載入檔案start.php。

start.php**如下:

namespace think;   

// 定義think命名空間,所有thinkphp類都在think及其子命名空間下。程式中用到框架類的時候要先use該類的命名空間;

// thinkphp 引導檔案

// 1. 載入基礎檔案

require __dir__ . '/base.php』;   

// 主要完成了常量定義、註冊自動載入、註冊錯誤和異常處理機制、載入慣例配置檔案(convention.php)這些步驟。

// 2. 執行應用

做了三件事:

1、定義整個框架的命名空間think

2、載入檔案base.php

而在base.php中,主要做了如下六件事:

1、定義系統常量,包括框架的執行資訊、目錄組織、執行環境等;

define('think_version', '5.0.12');

define('think_start_time', microtime(true));

define('think_start_mem', memory_get_usage());

define('ext', '.php');

define('ds', directory_separator);

defined('think_path') or define('think_path', __dir__ . ds);

define('lib_path', think_path . 'library' . ds);

define('core_path', lib_path . 'think' . ds);

define('trait_path', lib_path . 'traits' . ds);

defined('extend_path') or define('extend_path', root_path . 'extend' . ds);

defined('vendor_path') or define('vendor_path', root_path . 'vendor' . ds);

defined('runtime_path') or define('runtime_path', root_path . 'runtime' . ds);

defined('log_path') or define('log_path', runtime_path . 'log' . ds);

defined('cache_path') or define('cache_path', runtime_path . 'cache' . ds);

defined('temp_path') or define('temp_path', runtime_path . 'temp' . ds);

defined('conf_ext') or define('conf_ext', ext); // 配置檔案字尾

defined('env_prefix') or define('env_prefix', 'php_'); // 環境變數的配置字首

// 環境常量

define('is_cli', php_sapi == 'cli' ? true : false);

define('is_win', strpos(php_os, 'win') !== false);

2、載入loader類(/thinkphp/library/think/loader.php)

// 載入loader類

require core_path . 'loader.php';

3、載入環境變數配置檔案(/.env),如果該檔案不存在,就跳過;

// 載入環境變數配置檔案

if (is_file(root_path . '.env'))

} else }}

4、註冊自動載入

// 註冊自動載入

\think\loader::register();

5、註冊錯誤和異常處理機制

// 註冊錯誤和異常處理機制

\think\error::register();

6、載入慣例配置檔案

// 載入慣例配置檔案

\think\config::set(include think_path . 'convention' . ext);

ThinkPHP5 0 接觸學習

mvc不是設計模式,而是設計典範。composer 是 php5.3以上 的乙個依賴管理工具。它允許你宣告專案所依賴的 庫,它會在你的專案中為你安裝他們。開發環境介紹 php版本 5.4.0 pdo mbstring curl php extension 安裝好這個擴充套件,thinkphp5能正常...

ThinkPHP5 0學習 URL訪問

tp5支援path info和相容模式 示例 created by phpstorm.user wybing date 2019 7 16 time 14 13 class manager 通過path info方式訪問 通過相容模式方式訪問 輸入 推薦path info方式 url大小寫問題 1....

thinkphp5 0目錄結構

5.0的部署建議是public目錄作為web目錄訪問內容,其它都是web目錄之外,當然,你必須要修改public index.php中的相關路徑。如果沒法做到這點,請記得設定目錄的訪問許可權或者新增目錄列表的保護檔案。router.php用於php自帶webserver支援,可用於快速測試 啟動命令...