Skip to content

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:

KeyTypeDefaultDescription
base_pathstringgetcwd()Project root used for .env, config/app.php, storage, and base_path() resolution.
view_pathstringnoneDefines NEXPHANT_VIEW_PATH for the view engine.
workersintautoNumber of HTTP worker processes. When greater than 1, supervisor mode is enabled by default.
supervisorboolworkers > 1Enables the framework process supervisor when pcntl_fork is available.
runtime_modestringautoRuntime profile: auto, low_latency, balanced, throughput, or benchmark.
event_loopstringautoEvent loop backend: auto, event, ev, uv, or select.
socket_driverstringautoSocket backend: auto, native, or stream.
runtime_safetybooltrueEnables runtime context, ownership, drain tracking, and lifecycle discipline.
performance_modeboolfalseShortcut that disables runtime safety unless runtime_safety is set.
runtime_disciplineboolfollows runtime_safetyExplicitly controls lifecycle discipline.
runtimearraydefaultsFine-grained safety toggles: context, ownership, and drain_tracking.
metricsarraydefaultsMetrics options: enabled, sample_rate, histogram, and route_latency.
runtime_featuresarrayprofileRuntime feature flags merged into the selected profile.
resource_traceboolfalseCaptures resource ownership trace data.
leak_detectionboolfalseEnables leak detection support.
query_cache_ttlfloatnoneEnables database query cache with the given TTL.
query_coalesceboolnoneEnables database query coalescing.
auto_optimizebooltrueBoots route hot-path optimization cache during application boot.
max_connectionsintautoPer-worker connection limit derived from file descriptor limits.
backlogintautoTCP listen backlog derived from connection and worker counts.
keep_alive_timeoutintprofileKeep-alive idle timeout in seconds.
keep_alive_max_requestsintprofileMaximum requests served by one keep-alive connection.
max_accept_per_tickintprofileMaximum accepted sockets per loop tick.
max_read_callbacks_per_tickintprofileRead callback fairness limit per loop tick.
max_write_callbacks_per_tickintprofileWrite callback fairness limit per loop tick.
max_deferred_per_tickintprofileDeferred callback fairness limit per loop tick.
graceful_shutdown_timeoutint30Seconds allowed for in-flight requests to finish during shutdown.
debugboolfalseEnables debug-oriented server behavior.
websocket_*mixeddefaultsWebSocket timeout, ping, bus, Redis, frame, buffer, and backpressure options.
sse_*mixeddefaultsSSE 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): Route

group ​

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'
): int

runtime ​

Expose runtime endpoints:

php
$app->runtime(string $prefix = '/api'): self

Endpoints registered under the prefix:

EndpointDescription
/liveLiveness status.
/readyReadiness plus database health.
/healthRuntime and database stats.
/metricsPrometheus 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): self

Example:

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): self

Example:

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');

Released under the MIT License.