Tutorials
Tutorial 1: Hello World
A Taphra page and a JSON endpoint, running on your machine in a few minutes.
Prerequisites
#- PHP 8.2 or later (
php -v) - Composer
Step 1: Create the app
#composer create-project taphra/skeleton my-app
cd my-appComposer installs the framework, writes a random APP_SECRET_KEY to .env
and creates the app/tmp/ folders. The app already has a home page, a layout
(app/Layouts/default.lyt.php) and a JSON endpoint at /health.
Step 2: A controller
#A controller is a class in app/Controllers/; each public method is an
action. Name the file after the class, so Composer's autoloader finds it
without any extra step.
<?php
namespace App\Controllers;
class HelloController extends AppController
{
// An HTML page: sets the view's variables and renders
// app/Views/hello/world.tpl.php inside the default layout.
public function world(): void
{
$this->viewTitle = 'Hello';
$this->who = 'World';
$this->autoRender = true;
}
// A JSON answer: an action that returns a string sends it as the body.
public function greeting(): string
{
header('Content-Type: application/json');
return json_encode(['message' => 'Hello, World!']);
}
}AppController (in the same folder) is the base class of the app's HTML
controllers. Every public property you set in an action becomes a variable in
the view: $this->who is $who there.
Step 3: The view
#<h1>Hello, <?= e($who) ?>!</h1>
<p>Taphra is running.</p>Print values with e(), which escapes them for HTML (once: the view has
already escaped plain strings, and e() knows not to do it twice).
Step 4: Routes
#Without a route, /hello/world already reaches HelloController::world()
(the /controller/action convention). Routes give actions their own URLs and
say which HTTP methods they take. Replace app/Config/routes.yml with:
GET:
/health: health/index
/hello: hello/world
/hello/json: hello/greetingEach section is an HTTP method (GET, POST, PUT, PATCH, DELETE);
each entry maps a path to controller/action. A path declared for one method
answers other methods with 405 Method Not Allowed.
Step 5: Run it
#php taphra servephp taphra serve starts PHP's built-in server on http://127.0.0.1:8000 in
development mode, with clean URLs. Open http://127.0.0.1:8000/hello in your
browser, or, in a second terminal:
curl -s http://127.0.0.1:8000/hello | grep -o '<h1>.*</h1>'<h1>Hello, World!</h1>curl -s http://127.0.0.1:8000/hello/json{"message":"Hello, World!"}The page takes GET only:
curl -s -o /dev/null -w '%{http_code}\n' -X POST http://127.0.0.1:8000/hello405Stop the server with Ctrl+C.
What you built
#| File | Role |
|---|---|
app/Controllers/HelloController.php |
the actions behind /hello and /hello/json |
app/Views/hello/world.tpl.php |
the HTML of the world action |
app/Config/routes.yml |
URLs and HTTP methods for the actions |
app/Layouts/default.lyt.php |
the page around every view (it came with the skeleton) |
Before you deploy
#An unset APP_ENV means production: no debug pages. php taphra serve sets
APP_ENV=dev for its own server only. In production, point the web server's
document root at app/Static/ and set base_url in app/Config/app.yml; the
skeleton's README has the details.
Next steps
#- A JSON API with a database: Tutorial 2: REST API
- A chat page on an AI model: Tutorial 3: AI chatbot