ThinkPHP Mobile (TPM) 讓開發者一次開發、三端運行:PC 站、手機 Web、原生 Hybrid App。基於 ThinkPHP 模板引擎,打包為可安裝的 App,支援調用原生能力(相機、通訊錄等)。
| 類型 | 開發語言 | 跨平台 | 原生能力 | 靜態資源 | 安裝方式 |
|---|---|---|---|---|---|
| WebApp | HTML/CSS/JS | ✓ | ✗ | 服務器 | 瀏覽器訪問 |
| NativeApp | ObjC/Java/C# | ✗ | ✓ | 本地 | 應用商店 |
| HybridApp (TPM) | HTML/CSS/JS | ✓ | ✓ | 本地 | 應用商店/直裝 |
# GitHub https://github.com/liu21st/extend/tree/master/Extend/Tool/TPM
Tpl/ 目錄下文件 → 項目 Tpl/ SwitchMobileTplBehavior.class.php → Lib/Behavior/ TemplateMobile.class.php → ThinkPHP/Extend/Driver/Template/
// 配置文件 'LAYOUT_ON' => true // Conf/tags.php return ['action_begin' => ['SwitchMobileTpl']];
// ThinkPHP/Common/functions.php
function redirect($url, $time=0, $msg='') {
// ... 原有代碼 ...
if (0 === $time) {
if (defined('IS_CLIENT') && IS_CLIENT) {
if ('' !== __APP__) $url = substr($url, strlen(__APP__));
header('redirect: ' . $url); // App 專用 header
} else {
header('Location: ' . $url);
}
}
// ...
}
// Tpl/index.html
TPM.run("https://your-project-url");
cd /path/to/Tpl php build.php [platform] [name] [package] [version] # 參數說明 platform: android/ios (預設 android) name: 應用名稱 (預設 TPM) package: 包名如 com.think.yourname (預設 cn.thinkphp.tpm) version: 版本號 (預設 1.0) # 示例 php build.php android MyApp com.mycompany.myapp 1.0.0
需啟用 zip、curl 擴展,需聯網。離線可用 PhoneGap 打包。
正在加載中...
volist, foreach, for, empty, notempty, present, notpresent, eq, neq, heq, nheq, egt, gt, elt, lt, if, elseif, else, switch, case, default, in, notin, between, notbetween, include
<include file="Action:method" />defined、define__URL__、__PUBLIC__ 等/Action/method'TPM_THEME' => 'mobile' // Tpl/mobile/ 目錄下放置 App 專用模板 // 瀏覽器訪問用 Tpl/Index/index.html // App 訪問用 Tpl/mobile/Index/index.html
TPM.run({
api_base: 'https://www.xxx.com/index.php', // 入口地址
api_index: '/Index/login' // 首頁接口(預設 /Index/index)
});
// 隱藏入口文件時
TPM.run({
api_base: 'https://www.xxx.com', // 末尾無斜槓
api_index: '/Index/login'
});
// 簡寫
TPM.run('https://www.xxx.com');
// 錯誤:$(document).ready 監聽不到動態生成元素
$(document).ready(function(){ $('#id').click(...) });
// 正確:TPM.ready 在每次模板渲染後觸發
TPM.ready(function($){
$('#id').click(function(){ alert('click'); });
});