Documentation

On this page

Download full book (text)

# **Chapter 27: MobileGate — Hybrid Android Apps from Front Files** > **Prerequisites:** Chapters 5–6 (FrontFiles), Chapter 13 (AJAX / jsServer), > Chapter 22 (NativeGate overview — MobileGate is the phone-side twin) > **Try it:** Separate project `mobileapp/` (not runnable in the browser docs site). > Generate WebView HTML + Android Studio module from the CLI: > `cd mobileapp` then `php -f start.php -- --gate index`. > Open `mobileapp/` in Android Studio to build/run the APK. > Preview generated SPA HTML at `mobileapp/www/index.html` (static file). --- ## **27.1 Why a Separate Mobile Project** A **MobileGate** app is not an Apache page. PHP designs Front Files and settings; `start.php` **generates** a WebView shell (`www/`) plus an Android Studio module (`app/`). The APK has **no PHP server** inside — device logic runs as **KotlinGates**; remote PHP Gates stay reachable over HTTP/WebSocket (Chapters 13 and 22). | | Web (`BasicGate`) | Mobile (`MobileGate`) | |--|-------------------|------------------------| | Project | Site root / `apps/examples` | Dedicated folder e.g. `mobileapp/` | | Output | HTTP HTML via Master | `www/` + Android `app/` | | Run | Browser under Apache/SphpServer | Generate → Android Studio / device | | Native | Optional NativeGate WS | Kotlin Gates + WebView bridge | Reference layout in this repo: `mobileapp/`. ## **27.2 Project Layout** ``` mobileapp/ ├─ apps/ │ ├─ Index.gate.php # extends MobileGate │ ├─ fronts/ # .front → compiled into www/ │ ├─ sjs/ # client SJS per front (Chapter 28) │ ├─ kotlin_apps/ # KotlinGate classes (Chapter 28) │ ├─ android/ # templates (MainActivity, config.xml, …) │ └─ cpp/ # optional NDK sources ├─ www/ # GENERATED WebView assets ├─ app/ # GENERATED Gradle module ├─ start.php, comp.php, reg.php, masters/ └─ build.gradle.kts, settings.gradle.kts, gradlew ``` Package id defaults to `com.sartajphp.{foldername}` → `com.sartajphp.mobileapp`. Edit **source** under `apps/`. Treat `www/` and most of `app/src/main/` as generated — regenerate after Front/Gate changes. ## **27.3 Index Gate (`page_new`)** Home UI is always `apps/fronts/index_main.front` (`MobileGate` builds `FrontFile('main')`). Extra screens use `addPage`. Dist libs copy CSS/JS trees into `www/sphpmob`. ```php <?php // mobileapp/apps/Index.gate.php (excerpt, tested) class Index extends \Sphp\Gates\MobileGate { public function page_new() { $this->mobappname = "SphpDoc Mobile Demo"; $this->minSdkVersion = "26"; $this->targetSdkVersion = "34"; $this->compileSdkVersion = "34"; $this->addAndroidSetting( "permission", '<uses-permission android:name="android.permission.INTERNET"/>' ); $this->addAndroidSetting( "dependency", 'implementation("androidx.appcompat:appcompat:1.7.0")' ); SphpJsM::addBootStrap(); $this->addDistLib($this->resourcePath . "/jslib/twitter/bootstrap5"); $this->addDistLib($this->resourcePath . "/jslib/onsen"); $this->addCppDir(__DIR__ . "/cpp/nativedemo"); $this->addPage(new Sphp\Tools\FrontParser($this->myPath . "/fronts/page2.front")); $this->setSpecialMetaTag($cspMeta); // allow remote AJAX/WS hosts } } ``` Namespace: `\Sphp\Gates\MobileGate` (SphpDoc stub). Some apps keep a local copy named `AndroidMobileGate` — same responsibilities. ## **27.4 Important `MobileGate` APIs** | API | Purpose | |-----|---------| | `addPage` / `addDistLib` | Extra fronts + copy JS/CSS into `www/sphpmob` | | `addAndroidSetting($section, $line)` | `permission`, `application`, `dependency`, `plugins`, `android`, … | | `addAndroidLibFile` / `addAndroidLibDir` | AAR/libs → `app/libs` | | `addKotlinGate` / `addKotlinDir` / `addCppDir` / `addJavaDir` | Native sources | | `setSpecialMetaTag` | WebView CSP / meta | | `enableAndroidStudio` (default on) | Publish into project `app/` | | `disableAndroidStudio` | Alternate `build/` + auto `gradlew assembleDebug` | | `setForceCopyGen` | Force-refresh templates from `jslib/mobileapp/android` | | `enableSphpMobile` | Optional on-device PHP assets | App metadata properties: `mobappname`, `mobappversion`, `mobappid`, `mobappdes`, `mobappauthor`, SDK version fields above. ## **27.5 Generate and Build** From `mobileapp/`: ```bat php -f start.php -- --gate index ``` Generate writes `www/index.html` (and root `index.html`), syncs assets into `app/src/main/assets`, Manifest/`config.xml`, `app/build.gradle.kts`, Kotlin gates, and `regapp.json`. Then open **`mobileapp/`** in Android Studio → Sync → Run on a device/emulator. Full NDK/APK compile needs Android Studio + NDK on the machine when you use `addCppDir`. ## **27.6 Talking to a Website from the Phone** The APK has no local PHP Gate, but the WebView can call a **remote SartajPhp site** the same way a browser does: | Client API | Transport | Server | |------------|-----------|--------| | `sendAjax` / `getAjaxReturn` | HTTP AJAX | Remote `BasicGate` + `$this->jsServer` | | `callNativeGate` | WebSocket | Remote `NativeGate` on SphpServer | Allow those hosts in CSP via `setSpecialMetaTag` (`connect-src`, `default-src`, …). Keep Android `INTERNET` permission. Pin real `https://` / `wss://` hosts in production (the demo may use wildcards for local experiments). ## **27.7 Checklist** 1. Work in a dedicated folder with its own `start.php` / `reg.php`. 2. Index extends `\Sphp\Gates\MobileGate`; set `mobappname` and SDK levels. 3. Design `index_main.front`; `addPage` for extra screens. 4. `addDistLib` for Bootstrap/Onsen (and any other asset trees you need). 5. `php -f start.php -- --gate index` → inspect `www/` + `app/`. 6. Build/run in Android Studio — not as a Gate URL on this docs site. ## **27.8 Next Steps** - **Chapter 28** — Onsen SPA navigation, SJS, KotlinGates, C++/JNI - Chapter 13 / 22 — remote `sendAjax` and `callNativeGate` from the WebView - Chapter 22 §22.4 — short NativeGate ↔ mobile overview (this chapter is the full MobileGate tutorial) --- **Summary:** MobileGate turns Front Files into a hybrid Android WebView app. Generate from CLI; build with Android Studio. KotlinGates own on-device events; remote PHP Gates stay available over AJAX/WebSocket once CSP and permissions allow them.