Hello, World API
The smallest possible Lift app — JSON in, JSON out.
<?php
require 'vendor/autoload.php';
use Lift\App;
use Lift\Http\Response;
$app = new App();
$app->get('/', fn() => Response::json(['hello' => 'world']));
$app->run();
Small, copy-pasteable recipes that show how Lift fits real problems.
The smallest possible Lift app — JSON in, JSON out.
<?php
require 'vendor/autoload.php';
use Lift\App;
use Lift\Http\Response;
$app = new App();
$app->get('/', fn() => Response::json(['hello' => 'world']));
$app->run();
A typed controller resolved automatically through the container.
<?php
use Lift\App;
use Lift\Http\Request;
use Lift\Http\Response;
class UserController
{
public function __construct(private readonly UserRepository $users) {}
public function index(): array
{
return $this->users->all(); // -> auto JSON
}
public function show(Request $r): Response
{
$user = $this->users->find((int) $r->param('id'));
return $user
? Response::json($user)
: Response::json(['error' => 'Not found'], 404);
}
public function store(Request $r): Response
{
$id = $this->users->create($r->json());
return Response::json(['id' => $id], 201);
}
}
$app = new App();
$app->group('/api/v1', function ($g) {
$g->get('/users', [UserController::class, 'index']);
$g->get('/users/{id:\d+}', [UserController::class, 'show']);
$g->post('/users', [UserController::class, 'store']);
});
$app->run();
Verify a Bearer token in middleware, attach the user to the request.
<?php
use Lift\Http\Response;
use Lift\Jwt\Jwt;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class AuthMiddleware implements MiddlewareInterface
{
public function __construct(private readonly Jwt $jwt) {}
public function process(ServerRequestInterface $r, RequestHandlerInterface $h): ResponseInterface
{
[, $token] = explode(' ', $r->getHeaderLine('Authorization') . ' ', 2);
try {
$claims = $this->jwt->decode($token);
} catch (\Throwable) {
return Response::json(['error' => 'Unauthorized'], 401);
}
return $h->handle($r->withAttribute('user', $claims));
}
}
$app->singleton(Jwt::class, fn() => new Jwt(secret: $_ENV['APP_KEY']));
$app->get('/me', fn(Request $r) => $r->getAttribute('user'))
->middleware(AuthMiddleware::class);
Fluent rules — failures auto-return 422 with the error map.
<?php
$app->post('/signup', function (Request $r) {
// validate() merges body + query + route params, throws ValidationException on failure
$data = $r->validate([
'email' => 'required|email',
'password' => 'required|min_length:8',
'age' => 'integer|min:13',
]);
return Response::json(['user' => createUser($data)], 201);
});
Push a job from a handler; worker picks it up.
<?php
use Lift\Queue\AbstractJob;
use Lift\Queue\RedisQueue;
use Lift\Redis\RedisClient;
// AbstractJob supplies failed()/getQueue()/getDelay()/getTries();
// you only implement handle().
class SendWelcomeEmail extends AbstractJob
{
public function __construct(private string $email) {}
public function handle(): void
{
// ... send the email
}
}
$app->setQueue(new RedisQueue($app->make(RedisClient::class)));
$app->post('/signup', function (Request $r) use ($app) {
$email = $r->input('email');
$app->dispatch(new SendWelcomeEmail($email));
return Response::noContent();
});
Bind a JsonRpcServer to a single route — full spec compliance.
<?php
use Lift\JsonRpc\JsonRpcServer;
$rpc = new JsonRpcServer();
// Params are injected by name (params object) or position (params array)
$rpc->register('math.add', fn(int $a, int $b): int => $a + $b);
$rpc->register('math.mul', fn(int $a, int $b): int => $a * $b);
$app->rpc('/rpc', $rpc);
Stream live updates to the client.
<?php
use Lift\Http\SseEmitter;
use Lift\Http\SseEvent;
use Lift\Http\SseResponse;
$app->get('/stream/clock', function () {
return new SseResponse(function (SseEmitter $emit) {
while (true) {
$emit(SseEvent::json(['ts' => date('c')]));
sleep(1);
}
});
});
Drop-in workers for RoadRunner, Swoole, and FrankenPHP — keep PHP alive between requests with zero changes to your application code.
PHP stays alive between requests — no bootstrap cost per hit. Lift auto-detects the installed PSR-17 factory (Nyholm → Guzzle → Laminas).
<?php
// public/worker.php
require 'vendor/autoload.php';
use Lift\App;
use Lift\Http\Response;
use Lift\Runtime\RoadRunnerWorker;
$app = new App();
$app->get('/', fn() => Response::json(['status' => 'ok']));
$app->get('/users/{id:\d+}', fn(\Lift\Http\Request $r) => ['id' => $r->param('id')]);
(new RoadRunnerWorker($app))->serve();
.rr.yaml → server: {command: "php public/worker.php"}
Lift converts \Swoole\Http\Request → Lift Request and writes the Lift Response back — your route handlers stay identical.
<?php
require 'vendor/autoload.php';
use Lift\App;
use Lift\Http\Response;
use Lift\Runtime\SwooleServer;
$app = new App();
$app->get('/', fn() => Response::json(['status' => 'ok']));
$app->get('/users/{id:\d+}', fn(\Lift\Http\Request $r) => ['id' => $r->param('id')]);
(new SwooleServer($app, [
'host' => '0.0.0.0',
'port' => 9501,
'worker_num' => swoole_cpu_num(),
]))->start();
The same public/index.php works in both FrankenPHP worker mode and classic php-fpm — no separate entry point needed.
<?php
require 'vendor/autoload.php';
use Lift\App;
use Lift\Http\Response;
use Lift\Runtime\FrankenPhpWorker;
$app = new App();
$app->get('/', fn() => Response::json(['status' => 'ok']));
// FrankenPHP worker mode: PHP loops on frankenphp_handle_request().
// Falls back to a single request cycle for php-fpm / php -S.
if (function_exists('frankenphp_handle_request')) {
(new FrankenPhpWorker($app))->serve();
} else {
$app->run();
}