App Reference
App::create()
php
$app = App::create();
$app = App::create(array $config = []);Creates the application singleton and applies runtime configuration before the server is booted.
Common configuration keys:
| Key | Type | Default | Description |
|---|---|---|---|
base_path | string | getcwd() | Project root used for .env, config/app.php, storage, and base_path() resolution. |
view_path | string | none | Defines NEXPHANT_VIEW_PATH for the view engine. |
workers | int | auto | Number of HTTP worker processes. When greater than 1, supervisor mode is enabled by default. |
supervisor | bool | workers > 1 | Enables the framework process supervisor when pcntl_fork is available. |
runtime_mode | string | auto | Runtime profile: auto, low_latency, balanced, throughput, or benchmark. |
event_loop | string | auto | Event loop backend: auto, event, ev, uv, or select. |
socket_driver | string | auto | Socket backend: auto, native, or stream. |
runtime_safety | bool | true | Enables runtime context, ownership, drain tracking, and lifecycle discipline. |
performance_mode | bool | false | Shortcut that disables runtime safety unless runtime_safety is set. |
runtime_discipline | bool | follows runtime_safety | Explicitly controls lifecycle discipline. |
runtime | array | defaults | Fine-grained safety toggles: context, ownership, and drain_tracking. |
metrics | array | defaults | Metrics options: enabled, sample_rate, histogram, and route_latency. |
runtime_features | array | profile | Runtime feature flags merged into the selected profile. |
resource_trace | bool | false | Captures resource ownership trace data. |
leak_detection | bool | false | Enables leak detection support. |
query_cache_ttl | float | none | Enables database query cache with the given TTL. |
query_coalesce | bool | none | Enables database query coalescing. |
auto_optimize | bool | true | Boots route hot-path optimization cache during application boot. |
max_connections | int | auto | Per-worker connection limit derived from file descriptor limits. |
backlog | int | auto | TCP listen backlog derived from connection and worker counts. |
keep_alive_timeout | int | profile | Keep-alive idle timeout in seconds. |
keep_alive_max_requests | int | profile | Maximum requests served by one keep-alive connection. |
max_accept_per_tick | int | profile | Maximum accepted sockets per loop tick. |
max_read_callbacks_per_tick | int | profile | Read callback fairness limit per loop tick. |
max_write_callbacks_per_tick | int | profile | Write callback fairness limit per loop tick. |
max_deferred_per_tick | int | profile | Deferred callback fairness limit per loop tick. |
graceful_shutdown_timeout | int | 30 | Seconds allowed for in-flight requests to finish during shutdown. |
debug | bool | false | Enables debug-oriented server behavior. |
websocket_* | mixed | defaults | WebSocket timeout, ping, bus, Redis, frame, buffer, and backpressure options. |
sse_* | mixed | defaults | SSE heartbeat, timeout, replay, bus, Redis, and auth options. |
Example:
php
$app = App::create([
'base_path' => dirname(__DIR__),
'workers' => 4,
'runtime_mode' => 'balanced',
'event_loop' => 'auto',
'socket_driver' => 'auto',
'runtime_safety' => true,
]);get / post / put / patch / delete / options / any
php
$app->get(string $path, callable $handler, callable ...$middleware): Route
$app->post(string $path, callable $handler, callable ...$middleware): Route
$app->put(string $path, callable $handler, callable ...$middleware): Route
$app->patch(string $path, callable $handler, callable ...$middleware): Route
$app->delete(string $path, callable $handler, callable ...$middleware): Route
$app->options(string $path, callable $handler, callable ...$middleware): Route
$app->any(string $path, callable $handler, callable ...$middleware): Routegroup
php
$app->group(string $prefix, callable $callback, callable ...$middleware): self
$app->group('/api', function ($app) {
$app->get('/users', [UserController::class, 'index']);
}, new ApiMiddleware());use
Register global middleware:
php
$app->use(callable $middleware): self
$app->use(new Cors());static
Register static file middleware:
php
$app->static(string $root, array $options = []): self
$app->static(base_path('public'), ['max_age' => 86400, 'etag' => true]);ws
Register a WebSocket route:
php
$app->ws(
string $path,
callable $onMessage,
?callable $onOpen = null,
?callable $onClose = null
): self
$app->ws('/chat', fn ($conn, string $message) => $conn->send($message));sse
Register and broadcast Server-Sent Events:
php
$app->sse(string $path = '/events'): self
$app->broadcastSse(
string $path,
mixed $data,
?string $event = 'message',
string $channel = 'global'
): intruntime
Expose runtime endpoints:
php
$app->runtime(string $prefix = '/api'): selfEndpoints registered under the prefix:
| Endpoint | Description |
|---|---|
/live | Liveness status. |
/ready | Readiness plus database health. |
/health | Runtime and database stats. |
/metrics | Prometheus text metrics. |
annotations
Auto-register controllers from a directory:
php
$app->annotations(string|object|array $controller): self
$app->annotations(UserController::class);
$app->annotations([UserController::class, PostController::class]);Runtime Tuning
php
$app->workers(int $count): self
$app->supervisor(bool $enabled): self
$app->runtimeMode(string $mode): self
$app->eventLoop(string $backend): self
$app->socketDriver(string $driver): self
$app->maxAcceptPerTick(int $max): self
$app->tune(array $config): selfExample:
php
$app->workers(4)
->supervisor(true)
->runtimeMode('throughput')
->eventLoop('event')
->socketDriver('native')
->tune(['keep_alive_timeout' => 30]);Fast Responses
Register static hot-path responses:
php
$app->fastJson(string $method, string $path, array|string $payload, int $status = 200, array $headers = []): self
$app->fastText(string $method, string $path, string $body, int $status = 200, array $headers = []): self
$app->fastRaw(string $method, string $path, string $rawResponse): selfExample:
php
$app->fastJson('GET', '/health', ['status' => 'ok']);
$app->fastText('GET', '/robots.txt', "User-agent: *\nAllow: /\n");Fast responses are registered before normal request dispatch and are intended for fixed paths. Parameterized paths such as /users/{id} are ignored by the fast route path and should use normal routes.
listen
Start the HTTP server:
php
$app->listen(int $port = 8080, string $host = '0.0.0.0'): void
$app->listen(port: 8080);
$app->listen(port: 443, host: '0.0.0.0');