This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
fishyboat21/simple-api is a minimal PHP 8.4+ library for building JSON HTTP APIs using a per-file endpoint pattern. Each API endpoint is a single PHP file in public/api/ — the web server serves each file directly. No front controller, no route cache, no code generation.
No build step, no framework, no ORM — just Composer autoloading.
PSR-4 mapping: Fishyboat21\SimpleApi\ → src/
After adding new classes under src/, run:
composer dump-autoloadThe shared helper class used by every endpoint file. Each public/api/*.php file:
- Requires Composer autoloader
- Creates an
Endpointinstance (optionally with aCorsConfig) - Registers handlers for HTTP methods (
get(),post(),put(),delete(),patch()) - Calls
run()
The Endpoint::run() pipeline:
- Sets
Content-Type: application/json - Creates
Request::fromGlobals() - Handles CORS preflight (returns 204 for OPTIONS with Origin)
- Finds handler matching request method (lowercase key)
- Calls handler, wraps non-Response return values in
Response::ok() - Applies CORS response headers
- Sends JSON response
Error handling:
MethodNotAllowedException→ 405 withAllowheaderHttpExceptionsubclasses → their status code and messageThrowable→ generic "Internal Server Error" (real message logged via optional PSR-3 logger, never leaked)
public/api/health.php → GET /api/health.php (or /api/health with rewrite)
public/api/users.php → GET,POST /api/users.php
public/api/task.php?id=42 → GET,PUT,DELETE /api/task.php?id=42
Each endpoint file registers handlers for one or more HTTP methods:
$api = new Endpoint();
$api->get(fn(Request $r) => Response::ok(['data' => ...]));
$api->post(fn(Request $r) => Response::created(['id' => 1]));
$api->run();Request—fromGlobals()/fromParts(), lazy JSON body parsing, query/body/header accessorsResponse— factory methods:ok(),created(),noContent(),notFound(),error(),setHeader()for CORS/custom headers
Configurable per-endpoint via new Endpoint(corsConfig: $cors). Defaults to allow all origins, all common methods.
HttpException (abstract)
├── NotFoundException → 404
└── MethodNotAllowedException → 405
Requires PHP >=8.4.
- No routing overhead — web server serves PHP files directly
- No route cache — nothing to generate, nothing to load
- Lazy body parsing —
php://inputread only when handler callsbody() - Fixed pipeline — CORS → dispatch → send
- Zero filesystem scanning — no directory traversal at runtime