Documentation menu

Start here

Getting started

This page takes you from an empty folder to a running Taphra app with one HTML page and one JSON endpoint. You need PHP and Composer; a database is optional until you add DataStores.

Requirements

#
  • PHP 8.2, 8.3 or 8.4 (the test suite runs on all three).
  • The PHP extensions curl, json, mbstring, pdo and sodium.
  • Composer 2.
  • For a database: the PDO driver for it (pdo_mysql, pdo_pgsql or pdo_sqlite).

Optional extensions: intl (full CLDR plural rules and number formats), pcntl (several queue workers and clean shutdown on SIGTERM), dom (the HTML sanitiser).

Create an app

#
Shell
composer create-project taphra/skeleton my-app
cd my-app
php taphra serve

create-project runs a post-install step that writes a random APP_SECRET_KEY to .env and creates the writable folders under app/tmp/. The skeleton's composer.json sets config.platform.php to 8.2.0, so the lock file it writes also installs on PHP 8.2 (Deployment explains when to raise it). php taphra serve starts PHP's built-in server on http://127.0.0.1:8000 (change it with --host and --port). Open / for the welcome page and /health for a JSON response.

An app runs in production mode unless the APP_ENV environment variable says otherwise: an unset or unknown value means production, with the plain error pages and no debug output. php taphra serve sets APP_ENV=dev for its server when the variable is not set, so you get the development error page while you work. Configuration and environments has the details.

What the skeleton contains

#
Text
my-app/
  app/
    bootstrap.php        runs on every request; sets the mode from APP_ENV
    Config/              app.yml, databases.yml, routes.yml, migrations.yml
    Controllers/         AppController (base), HomeController, HealthController, AppErrorController
    DataStores/          AppDataStore (base class for your tables)
    Layouts/             default.lyt.php
    Migrations/          your migrations
    Models/              AppModel (base)
    Static/              the document root: index.php, .htaccess, frankenphp-worker.php
    Views/               home/index.tpl.php, error404.tpl.php, error500.tpl.php
    tmp/                 file cache, logs, compiled config (must be writable)
  composer.json
  taphra                 the command line: php taphra list

Point your web server's document root at app/Static/. The .htaccess file there sends every request that is not a real file to index.php under Apache; php taphra serve does the same for the built-in server.

Nothing connects to a database until you use a DataStore. Cache, sessions and logs use files under app/tmp/.

A first page

#

Generate a controller:

Shell
php taphra make:controller Post

This writes app/Controllers/PostController.php. Give it an action:

app/Controllers/PostController.php
<?php
// app/Controllers/PostController.php
namespace App\Controllers;

class PostController extends AppController
{
    public function index(): void
    {
        $this->viewTitle  = 'Posts';
        $this->posts      = ['Hello, world', 'Second post'];
        $this->autoRender = true;
    }
}

and a view for it:

app/Views/post/index.tpl.php
<!-- app/Views/post/index.tpl.php -->
<h1>Posts</h1>
<ul>
<?php foreach ($posts as $title): ?>
    <li><?= e($title) ?></li>
<?php endforeach; ?>
</ul>

Open http://127.0.0.1:8000/post. With no route declared, the URL /post runs PostController::index(), and /post/show/5 would run show('5'). Public properties you set on the controller become variables in the view, and autoRender = true renders app/Views/post/index.tpl.php inside the default layout. Routing and controllers covers declared routes; Views and helpers covers templates and escaping.

PostController extends the skeleton's AppController, which checks a CSRF token on every request that is not GET, HEAD or OPTIONS. Put <?= $this->csrf() ?> inside each form.

Class files and autoloading

#

The skeleton's composer.json maps the App\ namespace to app/ (PSR-4), so a class in a file named after it, such as app/Controllers/PostController.php, loads straight away. The make:* commands write files that way. The skeleton's own classes use the older *.class.php suffix (HomeController.class.php), which Composer finds through a classmap instead; if you add a *.class.php file by hand, run composer dump-autoload before the class can be found.

A first JSON endpoint

#

The skeleton's HealthController is a JSON endpoint: it extends Taphra\RestController and returns $this->json().

app/Controllers/StatusController.php
<?php
// app/Controllers/StatusController.php
namespace App\Controllers;

use Taphra\RestController;

class StatusController extends RestController
{
    public function index(): string
    {
        return $this->json(['status' => 'ok', 'time' => gmdate('c')]);
    }
}

GET /status now answers {"status":"ok","time":"..."} with Content-Type: application/json. RestController does not check CSRF tokens: protect endpoints that change data with token authentication (Authentication). REST APIs covers resources, errors and generated CRUD controllers.

Other starters

#

Three more Composer project templates start from a different place:

Package What it contains
taphra/api-starter A JSON API: JWT register and login endpoints, a posts resource with ownership checks, a PSR-15 pipeline (CORS, security headers, JSON body parsing, rate limiting) and migrations, on SQLite by default (MySQL or PostgreSQL by editing app/Config/databases.yml).
taphra/ai-chat A chat page that streams model replies over Server-Sent Events, with conversation history, a prompt template, an example tool and app/Config/ai.yml. The page is closed until you sign in with the CHAT_PASSWORD that composer create-project writes to .env, and messages are rate limited per visitor.
taphra/mcp-server An MCP server: a posts controller whose methods are MCP tools, resources and prompts, the stdio transport (php taphra mcp:serve) and the Streamable HTTP endpoint at /mcp.
Shell
composer create-project taphra/mcp-server my-server

Next steps

#