Skip to main content

Overview

The Response class provides a fluent interface for creating and sending HTTP responses with proper status codes, headers, and content formatting.

Constructor

Create a new Response instance.
mixed
default:""
Response content (string, array, or any value convertible to string)
int
default:"200"
HTTP status code
array
default:"[]"
Associative array of HTTP headers

Example

Static Factory Methods

json

Create a JSON response.
mixed
required
Data to encode as JSON (typically an array)
int
default:"200"
HTTP status code

Returns

Response
Response with Content-Type set to application/json
JSON encoding uses JSON_UNESCAPED_UNICODE and JSON_UNESCAPED_SLASHES flags for clean output.

Example

html

Create an HTML response.
string
required
HTML content
int
default:"200"
HTTP status code

Returns

Response
Response with Content-Type set to text/html

Example

text

Create a plain text response.
string
required
Plain text content
int
default:"200"
HTTP status code

Returns

Response
Response with Content-Type set to text/plain

Example

error

Create an error response with JSON format.
string
required
Error message
int
default:"500"
HTTP error status code

Returns

Response
JSON response with error message

Example

Instance Methods

send

Send the response to the client (outputs headers and content).

Example

setHeader

Set a custom HTTP header.
string
required
Header name
string
required
Header value

Returns

Response
Same Response instance for method chaining

Example

getHeader

Get a header value from the response.
string
required
Header name
string|null
default:"null"
Default value if header doesn’t exist

Returns

string|null
Header value or default

Example

getStatusCode

Get the HTTP status code.

Returns

int
HTTP status code

Example

getContent

Get the response content as a string.

Returns

string
Response content

Example

HTTP Status Codes

Common status codes to use with Response methods:

Success (2xx)

Client Errors (4xx)

Server Errors (5xx)

Custom Headers

CORS Headers

Cache Control

Custom API Headers

Response Patterns

RESTful API Responses

Error Handling

Conditional Responses

Paginated Responses

Complete Example