Response
in package
Response - HTTP Response Management
Encapsulates HTTP headers, status codes and redirections to avoid direct use of header() and http_response_code().
Features:
- HTTP status code management
- Custom headers
- Redirections (302, 301, etc.)
- JSON responses
- Content-Type management
Tags
Table of Contents
Properties
- $headers : array<string, string>
- $statusCode : int
Methods
- getHeaders() : array<string, string>
- Get all HTTP headers
- getStatusCode() : int
- Get the HTTP status code
- json() : never
- Send a JSON response
- redirect() : never
- Redirect to a URL
- send() : void
- Send HTTP headers
- setContentType() : self
- Set the Content-Type header
- setHeader() : self
- Set an HTTP header
- setStatusCode() : self
- Set the HTTP status code
Properties
$headers
private
array<string, string>
$headers
= []
$statusCode
private
int
$statusCode
= 200
Methods
getHeaders()
Get all HTTP headers
public
getHeaders() : array<string, string>
Return values
array<string, string> —All configured headers
getStatusCode()
Get the HTTP status code
public
getStatusCode() : int
Return values
int —Current status code
json()
Send a JSON response
public
json(mixed $data[, int $statusCode = 200 ]) : never
Sets Content-Type to application/json, encodes data and terminates.
Parameters
- $data : mixed
-
Data to encode as JSON
- $statusCode : int = 200
-
HTTP status code
Return values
never —Terminates execution
redirect()
Redirect to a URL
public
redirect(string $url[, int $statusCode = 302 ]) : never
Sends Location header and terminates script execution.
Parameters
- $url : string
-
Destination URL
- $statusCode : int = 302
-
Status code (302 = temporary, 301 = permanent)
Return values
never —Terminates execution
send()
Send HTTP headers
public
send() : void
Sends all configured headers. Safe to call multiple times (checks if headers already sent).
setContentType()
Set the Content-Type header
public
setContentType(string $contentType[, string $charset = 'UTF-8' ]) : self
Parameters
- $contentType : string
-
MIME type (e.g., text/html, application/json)
- $charset : string = 'UTF-8'
-
Character encoding (default: UTF-8)
Return values
self —For method chaining
setHeader()
Set an HTTP header
public
setHeader(string $name, string $value) : self
Parameters
- $name : string
-
Header name (e.g., Content-Type, Location)
- $value : string
-
Header value
Return values
self —For method chaining
setStatusCode()
Set the HTTP status code
public
setStatusCode(int $code) : self
Parameters
- $code : int
-
HTTP status code (200, 404, 500, etc.)
Return values
self —For method chaining