Documentation

On this page

Download full book (text)

# **Appendix A: Minimum Project Files — The Smallest Running SartajPhp App** > **Try it:** This whole documentation project IS a minimum app plus examples. > Strip everything except the files below and open `index.html` — > the framework still serves the default Index. --- ## **A.1 Required vs Optional** | File / folder | Role | Required? | |---------------|------|-----------| | `start.php` | Boot: paths + `$libVersion` → loads `res/Score/{lib}/global/start.php` | **Yes** | | `comp.php` | Project settings: `$debugMode`, `$sessionPath` / `$sessionHandler`, `$dbEnginePath`, `$db`, `$masterf`, secrets | **Yes** | | `prerun.php` | `SphpPreRun::onstart()` before every Gate | Yes (typical) | | `cachelist.php` | `addCacheList` registry (may be empty `<?php`) | Present (can be empty) | | `masters/` | Master layout (`*.mast.php`) | Yes for HTML pages | | `cache/` | Engine cache, shared sessions (`cache/ses` when `$sessionHandler='db'`), `Sphp_errorLog.txt` | As needed | | `vendor/sartajphp/sartajphp/` | The framework | **Yes** | | `reg.php` + `apps/` | Your Gates | **Optional** | | `db/` | SQL design files (`db_index.php`) | If you use a database | | `plugin/`, `uploads/` | Plugins / user files | As needed | Without any project Gate, the framework serves the shared `vendor/.../res/Slib/apps/Index.gate.php`. Adding `reg.php` + `apps/` replaces that with your app — which is exactly the moment "template" becomes "project". ## **A.2 start.php (tested, this project)** ```php <?php $sharedPath = "./vendor/sartajphp/sartajphp"; $resourceUrl = "./vendor/sartajphp/sartajphp/res"; $libVersion = "SphpLatest"; $slibVersion = "Slib"; // not editable start if(!defined("START_PATH")){ define("START_PATH", __DIR__); } // ... argv parsing (generated) ... $resourcePath = $sharedPath . "/res"; include_once("{$resourcePath}/Score/{$libVersion}/global/start.php"); $globalapp = startSartajPhpEngine(); if($globalapp != ""){ require_once($globalapp); SphpBase::engine()->execute(true); } // not editable end ``` Only the four variables at top are yours: where the framework lives, its URL, and which library version to boot (`SphpLatest` in this project). ## **A.3 comp.php Essentials (tested)** ```php <?php $cmpId = "demo"; // component/app id stored in rows (spcmpId) $cmpName = "SartajPHP Demo"; // Session storage (see A.3a) — this project shares sessions across Apache + SphpServer $sessionPath = './cache/ses'; $sessionHandler = 'db'; // '' = PHP defaults; 'db' = SQLite; or Redis URL $debugMode = 2; // Chapter 19 $admUser = 'admin'; // super-admin login (Chapter 14) $admPass = '...'; // keep OUT of public repos $dbUser = "root"; $dbPass = "mypass"; $dbEnginePath = __DIR__ . "/apps/examples/engines/SqlitePdoEngine.php,Sphp\Lib\SqlitePdoEngine"; $db = PROJ_PATH . "/db/spanel.db"; // SQLite file (or MySQL name) $projectUrl = ""; // sub-folder under webroot when not at domain root if($projectUrl != ""){ $projectUrl .= "/" . basename(__DIR__); } $masterf = "masters/default/master.mast.php"; // guest master $mebmasterf = "masters/default/master.mast.php"; // member master function getWelcome(){ /* role landing — Chapter 14 */ } ``` `$dbEnginePath` is `file.php,Class\Name` — the engine swap of Chapter 12 is literally this one line. `$projectUrl` is why every URL in this book uses `getGateUrl()`/`getEventUrl()` instead of hard-coded `/`. ## **A.3a Session Management (Web Server + SphpServer)** By **default**, SartajPHP leaves PHP session handling alone: empty `$sessionHandler` / `$sessionPath` means **PHP’s own settings** (files under `session.save_path`, etc.). That is fine for a single web server process. When the same visitor must stay logged in across **Apache/nginx (BasicGate)** and **SphpServer (NativeGate / WebSocket)**, both runtimes need to **read and write the same session store**. File sessions often fail here (different workers, locking, or paths). Configure a shared handler in `comp.php`: | `$sessionHandler` | Meaning | |-------------------|---------| | `''` (empty) | Use PHP settings only — no framework overwrite | | `'db'` | SQLite session handler (`SqliteSessionHandler`) under `$sessionPath` | | Redis URL (e.g. `tcp://127.0.0.1:6379`) | Redis session handler | ```php <?php // comp.php — shared sessions for BasicGate + NativeGate (this project) $sessionPath = './cache/ses'; // directory for the SQLite session DB file $sessionHandler = 'db'; // SQLite; readable by web server and SphpServer ``` What this enables: 1. Login on a normal HTTP Gate (`signin.html` / Chapter 14) writes `usertype`, `sid`, permissions into the shared store. 2. A NativeGate under SphpServer (Chapter 22) sees the **same** session keys via `$this->client->session(...)` / `SphpBase::sphpRequest()->session(...)`. 3. Auth cookies / remember-me still identify the session; the **payload** lives in SQLite (or Redis) instead of a process-local file. Notes: - Keep `cache/ses` (or your `$sessionPath`) writable by **both** the web-server user and the SphpServer process. - SQLite session mode also avoids the classic file-session “perma-freeze” lock under concurrent AJAX (see `Sphp\Kit\Session::useSqliteDb()`). - Redis is the high-traffic alternative: set `$sessionHandler` to the server URL instead of `'db'`. - Read/write API is unchanged either way: `$this->client->session('key')` / `$this->client->session('key', $value)`. Cross-refs: authentication keys in Chapter 14 · NativeGate sharing in Chapter 22 §22.5a. ## **A.4 prerun.php (tested)** Runs before every Gate — the place for headers and session-level switches: ```php <?php class SphpPreRun extends Sphp\core\SphpPreRunP{ public function onstart(){ $passtoken = "abc"; if(\SphpBase::sphpRequest()->session("edtmode") == $passtoken){ \SphpBase::sphpSettings()->blnEditMode = true; // Chapter 19 }else if(\SphpBase::sphpRequest()->request("edtmode") == $passtoken){ \SphpBase::sphpRequest()->session("edtmode",$passtoken); \SphpBase::sphpSettings()->blnEditMode = true; } } } ``` ## **A.5 reg.php + One Gate (tested pattern)** ```php <?php registerGate("index", __DIR__ . "/apps/Index.gate.php"); registerGate("blog", __DIR__ . "/apps/examples/Blog.gate.php"); // permlist form (Chapter 14): // registerGate("mydocs", __DIR__."/apps/MyDocs.gate.php", "", "My Docs", // array(["add","Add"],["delete","Delete"])); ``` Plus the fallback auto-register (this project's `reg.php` tail): an unregistered `foo.html` maps to `apps/foo.gate.php` if the file exists. ```php <?php // apps/Hello.gate.php — the smallest Gate use Sphp\Gates\BasicGate; class Hello extends BasicGate { public function onstart() { global $masterf; $this->setMasterFile($masterf); } public function page_new() { $this->setFrontFile(new \Sphp\Tools\FrontFile('hello')); // fronts/hello.front } } ``` ## **A.6 Boot Flow** ```mermaid flowchart TD start[start.php] --> eng[Engine boots] eng --> comp[comp.php] eng --> prerun[prerun.php onstart] eng --> cache[cachelist.php] eng --> reg{reg.php?} reg -->|yes| apps[apps/*.gate.php] reg -->|no| slib[Slib default Index] apps --> master[masters + fronts render] slib --> master ``` ## **A.7 Run It** Apache sub-folder (this book's setup): copy the minimum files under the webroot, set `$projectUrl`, open `index.html`. SphpServer: ```bat sphpdesk\sphpserver\sphpserver-win.exe --proj .\ --port 8000 --host 127.0.0.1 ``` CLI (Chapter 21): ```bat php -f start.php -- --gate index ``` --- **Previous:** [Chapter 23 — Component Internals](chapter23.md) · **Next:** [Appendix B — Working With AI Agents](appendixB.md)