HTTP Responses
ZealPHP wraps OpenSwoole's response with a clean API. Every method is coroutine-safe — no output buffering leaks across concurrent requests.
| Method | Signature | What it does |
|---|---|---|
json() | json($data, $status=200) | Sets Content-Type: application/json, encodes and ends response |
redirect() | redirect($url, $status=302) | Sets Location header + status, no body |
header() | header($key, $value) | Queues response header (sent on flush) |
cookie() | cookie($name, $value, ..., $samesite) | Sets cookie with full attributes incl. SameSite |
status() | status(int $code) | Sets HTTP status code |
stream() | stream(callable $fn) | Flush headers immediately, stream body via $write() closure |
sse() | sse(callable $fn) | Server-Sent Events — sets event-stream headers, $emit() closure |
end() | end(?string $data) | Send final body and close connection |
GET
json() — returns JSON with status 200
$app->route('/demo/response/json', function() {
return ['framework' => 'ZealPHP', 'async' => true, 'time' => time()];
// Returning an array auto-sets Content-Type: application/json
});
LIVE OUTPUT
Click Run →
GET
redirect() — 301 permanent redirect
$app->route('/demo/response/redirect-301', function($response) {
$response->redirect('/routing', 301);
});
LIVE OUTPUT
Click Run →
GET
header() — custom response headers
$app->route('/demo/response/headers', function($response) {
$response->header('X-Framework', 'ZealPHP');
$response->header('X-Async', 'true');
$response->header('Cache-Control','no-store');
return ['headers_set' => ['X-Framework', 'X-Async', 'Cache-Control']];
});
LIVE OUTPUT
Click Run →
GET
cookie() — SameSite cookie
$app->route('/demo/response/cookie', function($response) {
// Full PHP 7.3+ signature including SameSite
$response->cookie('session_demo', 'abc123', 0, '/', '', false, true, 'Strict');
return ['cookie_set' => 'session_demo=abc123; SameSite=Strict; HttpOnly'];
});
LIVE OUTPUT
Click Run →
Streaming responses — stream() and sse() are covered on the
Streaming page. They send headers immediately and bypass the PSR-7 output buffer.