Skip to content

Response Reference ​

JSON response ​

Return an array — automatically serialized to JSON:

php
return ['status' => 'ok'];
return ['user' => $user, 'meta' => $meta];

Explicit:

php
return Response::json(['status' => 'ok']);
return Response::json($data, status: 201);

Text response ​

php
return Response::text('Hello, world!');

HTML response ​

php
return Response::html('<h1>Hello</h1>');

Status code ​

php
return Response::json($data, status: 201);
return Response::json(['error' => 'Not found'], status: 404);
return Response::text('', status: 204);

Headers ​

php
return Response::json($data)
    ->withHeader('X-Request-Id', $traceId)
    ->withHeader('Cache-Control', 'no-store');

Redirect ​

php
return Response::redirect('/login');
return Response::redirect('/dashboard', status: 301);

No content ​

php
return Response::noContent(); // 204 No Content

File download ​

Send a file as a download attachment:

php
return Response::download('/var/www/storage/exports/report.csv', filename: 'report.csv');

The filename parameter sets the Content-Disposition: attachment header sent to the browser.

File response (inline) ​

Serve a file inline (e.g., images, PDFs):

php
return Response::file('/var/www/storage/images/photo.jpg');

MIME type is detected automatically from the file extension.

Cookies ​

Attach a cookie to any response:

php
return Response::json(['ok' => true])
    ->withCookie('session_id', $token, expires: time() + 3600)
    ->withCookie('theme', 'dark', expires: time() + 86400 * 30);

Delete a cookie:

php
return Response::json(['ok' => true])
    ->withoutCookie('session_id');

Streaming response ​

Stream a large response body without buffering it entirely in memory:

php
return Response::stream(function (StreamWriter $stream) {
    foreach ($largeDataset as $row) {
        $stream->write(json_encode($row) . "\n");
    }
}, contentType: 'application/x-ndjson');

Server-Sent Events (SSE) ​

Push events to a connected SSE client:

php
$app->sse('/events');

// From anywhere in the app
$app->broadcastSse('/events', ['message' => 'hello'], event: 'chat');

Raw response ​

Send a pre-formatted HTTP response string directly:

php
return Response::raw("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\npong");

Released under the MIT License.