ThinkPHP (以下簡稱 TP) 採用單一入口、模組化架構。理解 Common 目錄自動載入規則與 __PUBLIC__ 模板變數映射,對專案結構規劃至關重要。
Common/common.php 會被框架自動 include。function.php 或其他,需手動載入或配置。// @ 代表當前模組的 Common 目錄
load('@.function');
在模組配置檔 (Conf/config.php) 中:
'LOAD_EXT_FILE' => 'function', // 載入 Common/function.php
支援陣列一次載入多個:'LOAD_EXT_FILE' => ['function', 'helper']。
__PUBLIC__ 模板變數映射__PUBLIC__ 不是常數,而是模板引擎的字串替換規則,預設指向專案根目錄下的 Public/。
可在配置中自訂:
'TMPL_PARSE_STRING' => [
'__PUBLIC__' => __ROOT__ . '/Public', // 或其他路徑
'__STATIC__' => __ROOT__ . '/Static', // 自訂新標籤
],
模板中使用:<link href="__PUBLIC__/css/app.css" rel="stylesheet">。
Common/common.php,模組級放各模組 Common/。__STATIC__ 區分業務模組資源與框架資源。TMPL_PARSE_STRING 映射即可全站切換域名。