Overview
TheResponse 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/jsonJSON encoding uses
JSON_UNESCAPED_UNICODE and JSON_UNESCAPED_SLASHES flags for clean output.Example
- Basic JSON
- With Status Code
- Nested Data
html
Create an HTML response.string
required
HTML content
int
default:"200"
HTTP status code
Returns
Response
Response with Content-Type set to
text/htmlExample
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/plainExample
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
- 404 Not Found
- 400 Bad Request
- 500 Server Error
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