Response Methods
Helper Functions
Section titled “Helper Functions”Import from the Verge namespace:
use function Verge\response;use function Verge\json;use function Verge\html;use function Verge\redirect;use function Verge\file;use function Verge\download;| Function | Description |
|---|---|
response($body, $status, $headers) | Create Response with body, status, and headers |
json($data, $status, $headers) | Create JSON response with Content-Type header |
html($content, $status, $headers) | Create HTML response with Content-Type: text/html |
redirect($url, $status) | Create redirect response (default 302) |
file($path, $contentType) | Stream file inline (auto-detects MIME type) |
download($path, $filename, $contentType) | Force file download with Content-Disposition header |
Response Object Methods
Section titled “Response Object Methods”| Method | Description |
|---|---|
$response->body() | Get response body |
$response->status() | Get HTTP status code |
$response->header($name, $value) | Add or set header, returns new Response |
$response->getHeader($name) | Get header value(s) |
$response->json() | Parse response body as JSON array (useful for testing) |
Implicit Conversions
Section titled “Implicit Conversions”Verge automatically converts return values:
| Return Type | Response |
|---|---|
string | 200 OK, text/plain |
array | 200 OK, application/json |
null | 204 No Content |
Response | Used as-is |
Examples
Section titled “Examples”Plain Text
Section titled “Plain Text”app()->get('/hello', fn() => 'Hello World');app()->get('/users', fn() => ['data' => User::all()]);With Status Code
Section titled “With Status Code”use function Verge\json;
app()->post('/users', fn() => json(['id' => 1], 201));With Headers
Section titled “With Headers”use function Verge\response;
app()->get('/download', fn() => response('content') ->header('Content-Type', 'application/pdf') ->header('Content-Disposition', 'attachment'));Redirect
Section titled “Redirect”use function Verge\redirect;
app()->get('/old', fn() => redirect('/new'));app()->get('/moved', fn() => redirect('/new-home', 301));use function Verge\html;
app()->get('/welcome', fn() => html('<h1>Welcome</h1>'));app()->get('/error', fn() => html('<p>Not found</p>', 404));File Streaming
Section titled “File Streaming”use function Verge\file;
app()->get('/image', fn() => file('/path/to/image.png'));app()->get('/pdf', fn() => file('/path/to/doc.pdf', 'application/pdf'));File Download
Section titled “File Download”use function Verge\download;
app()->get('/export', fn() => download('/path/to/data.csv', 'users.csv'));app()->get('/report', fn() => download('/path/to/file.pdf', 'report.pdf', 'application/pdf'));