API Responses (`Core\Response`)
LilaPHP provides dedicated response handling: high-speed JSON payload delivery for `/api/*` endpoints with automated CORS and status headers, and server-rendered HTML views via `Core\View` for web routes.
1. `Response::json(array|object $data, int $status = 200, array $headers = [])`
Instantly encodes array or object data into JSON with CORS headers and terminates execution (`exit;`) to prevent buffer contamination.
use Core\Response;
Response::json([
'status' => 'success',
'data' => ['id' => 1, 'name' => 'Keyboard']
]);
Response::json(['status' => 'created', 'id' => 42], 201);
2. `Response::error(string $message, int $status = 400, ?array $errors = null)`
Emits a standardized API error structure containing status code, error message, and optional detailed validation errors array.
Response::error('Invalid parameters provided', 400);
Response::error('Validation failed', 422, [
'email' => ['The email field must be a valid email address.'],
'password' => ['The password must be at least 8 characters.']
]);
3. `Response::file(string $filePath, ?string $downloadName = null, bool $inline = false)`
Streams a physical file directly to the browser with automated MIME checking, CORS headers, and inline/attachment disposition.
Response::file(storage_path('reports/invoice_102.pdf'), 'Invoice-102.pdf');
Response::file(storage_path('images/avatar.png'), null, true);
4. `Response::stream(callable $callback, int $status = 200, string $contentType = '...')`
Disables output buffering and streams content in real-time (Server-Sent Events / SSE or chunked binary data).
Response::stream(function() {
for ($i = 1; $i <= 5; $i++) {
echo "data: " . json_encode(['progress' => $i * 20]) . "\n\n";
ob_flush();
flush();
sleep(1);
}
}, 200, 'text/event-stream');
5. `Response::redirect(string $url, int $status = 302)`
Issues HTTP redirect header (`Location: $url`) and terminates execution.
Response::redirect('/login');
Request Inspection Helpers (`Core\Request`)
Zero-allocation static helpers for retrieving memoized JSON payloads, input variables, Bearer tokens, headers, and client IP addresses:
use Core\Request;
$json = Request::json();
$page = Request::input('page', 1);
$action = Request::input('action', 'login');
$allInputs = Request::all();
$token = Request::bearerToken();
$apiKey = Request::header('X-API-Key');
$authHeader = Request::header('Authorization');
$headers = Request::headers();
$ip = Request::ip();
$method = Request::getMethod();
if (Request::isPost()) {
}
High-Concurrency RAM Caching (`Core\Cache`)
Performance FirstInstead of wrapping endpoints in heavy middleware decorators, LilaPHP lets you cache exact payloads or database queries inside shared RAM (`APCu` or `Redis`).
`Cache::api(string $key, callable $resolver, int $ttlSeconds)` (Shared Process RAM)
Stores data inside local worker RAM (`APCu`) with zero network latency. Ideal for read-heavy endpoints.
use Core\Cache;
use Core\Response;
$data = Cache::api('stats_dashboard_v1', function() {
return ['active_users' => 14250, 'load' => '0.12'];
}, 300);
Response::json($data);
`Request::GET(['cache' => true, 'cache_ttl' => 0], ...)` (Instant Route Caching)
Executes route middlewares first, then serves cached response payloads with zero latency. Set cache_ttl => 0 for infinite RAM caching.
use Core\Request;
use Core\Response;
Request::GET(['cache' => true, 'cache_ttl' => 0], function() {
return ['status' => 'ok', 'timestamp' => time()];
});