class Response

Response represents an HTTP response.

Constants

HTTP_CONTINUE

HTTP_SWITCHING_PROTOCOLS

HTTP_PROCESSING

HTTP_EARLY_HINTS

HTTP_OK

HTTP_CREATED

HTTP_ACCEPTED

HTTP_NON_AUTHORITATIVE_INFORMATION

HTTP_NO_CONTENT

HTTP_RESET_CONTENT

HTTP_PARTIAL_CONTENT

HTTP_MULTI_STATUS

HTTP_ALREADY_REPORTED

HTTP_IM_USED

HTTP_MULTIPLE_CHOICES

HTTP_MOVED_PERMANENTLY

HTTP_FOUND

HTTP_SEE_OTHER

HTTP_NOT_MODIFIED

HTTP_USE_PROXY

HTTP_RESERVED

HTTP_TEMPORARY_REDIRECT

HTTP_PERMANENTLY_REDIRECT

HTTP_BAD_REQUEST

HTTP_UNAUTHORIZED

HTTP_PAYMENT_REQUIRED

HTTP_FORBIDDEN

HTTP_NOT_FOUND

HTTP_METHOD_NOT_ALLOWED

HTTP_NOT_ACCEPTABLE

HTTP_PROXY_AUTHENTICATION_REQUIRED

HTTP_REQUEST_TIMEOUT

HTTP_CONFLICT

HTTP_GONE

HTTP_LENGTH_REQUIRED

HTTP_PRECONDITION_FAILED

HTTP_REQUEST_ENTITY_TOO_LARGE

HTTP_REQUEST_URI_TOO_LONG

HTTP_UNSUPPORTED_MEDIA_TYPE

HTTP_REQUESTED_RANGE_NOT_SATISFIABLE

HTTP_EXPECTATION_FAILED

HTTP_I_AM_A_TEAPOT

HTTP_MISDIRECTED_REQUEST

HTTP_UNPROCESSABLE_ENTITY

HTTP_LOCKED

HTTP_FAILED_DEPENDENCY

HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL

HTTP_TOO_EARLY

HTTP_UPGRADE_REQUIRED

HTTP_PRECONDITION_REQUIRED

HTTP_TOO_MANY_REQUESTS

HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE

HTTP_INTERNAL_SERVER_ERROR

HTTP_NOT_IMPLEMENTED

HTTP_BAD_GATEWAY

HTTP_SERVICE_UNAVAILABLE

HTTP_GATEWAY_TIMEOUT

HTTP_VERSION_NOT_SUPPORTED

HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL

HTTP_INSUFFICIENT_STORAGE

HTTP_LOOP_DETECTED

HTTP_NOT_EXTENDED

HTTP_NETWORK_AUTHENTICATION_REQUIRED

Properties

ResponseHeaderBag $headers
protected string $content
protected string $version
protected int $statusCode
protected string $statusText
protected string $charset
static array $statusTexts Status codes translation table.

Methods

__construct($content = '', int $status = 200, array $headers = array())

No description

static Response
create(mixed $content = '', int $status = 200, array $headers = array())

Factory method for chainability.

string
__toString()

Returns the Response as an HTTP string.

__clone()

Clones the current Response instance.

$this
prepare(Request $request)

Prepares the Response before it is sent to the client.

$this
sendHeaders()

Sends HTTP headers.

$this
sendContent()

Sends content for the current web response.

$this
send()

Sends HTTP headers and content.

$this
setContent(mixed $content)

Sets the response content.

string
getContent()

Gets the current response content.

$this
setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

string
getProtocolVersion()

Gets the HTTP protocol version.

$this
setStatusCode(int $code, $text = null)

Sets the response status code.

int
getStatusCode()

Retrieves the status code for the current web response.

$this
setCharset(string $charset)

Sets the response charset.

string|null
getCharset()

Retrieves the response charset.

bool
isCacheable()

Returns true if the response may safely be kept in a shared (surrogate) cache.

bool
isFresh()

Returns true if the response is "fresh".

bool
isValidateable()

Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request.

$this
setPrivate()

Marks the response as "private".

$this
setPublic()

Marks the response as "public".

$this
setImmutable(bool $immutable = true)

Marks the response as "immutable".

bool
isImmutable()

Returns true if the response is marked as "immutable".

bool
mustRevalidate()

Returns true if the response must be revalidated by caches.

DateTimeInterface|null
getDate()

Returns the Date header as a DateTime instance.

$this
setDate(DateTimeInterface $date)

Sets the Date header.

int
getAge()

Returns the age of the response in seconds.

$this
expire()

Marks the response stale by setting the Age header to be equal to the maximum age of the response.

DateTimeInterface|null
getExpires()

Returns the value of the Expires header as a DateTime instance.

$this
setExpires(DateTimeInterface $date = null)

Sets the Expires HTTP header with a DateTime instance.

int|null
getMaxAge()

Returns the number of seconds after the time specified in the response's Date header when the response should no longer be considered fresh.

$this
setMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh.

$this
setSharedMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh by shared caches.

int|null
getTtl()

Returns the response's time-to-live in seconds.

$this
setTtl(int $seconds)

Sets the response's time-to-live for shared caches in seconds.

$this
setClientTtl(int $seconds)

Sets the response's time-to-live for private/client caches in seconds.

DateTimeInterface|null
getLastModified()

Returns the Last-Modified HTTP header as a DateTime instance.

$this
setLastModified(DateTimeInterface $date = null)

Sets the Last-Modified HTTP header with a DateTime instance.

string|null
getEtag()

Returns the literal value of the ETag HTTP header.

$this
setEtag(string $etag = null, bool $weak = false)

Sets the ETag value.

$this
setCache(array $options)

Sets the response's cache headers (validation and/or expiration).

$this
setNotModified()

Modifies the response so that it conforms to the rules defined for a 304 status code.

bool
hasVary()

Returns true if the response includes a Vary header.

array
getVary()

Returns an array of header names given in the Vary header.

$this
setVary(string|array $headers, bool $replace = true)

Sets the Vary header.

bool
isNotModified(Request $request)

Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request.

bool
isInvalid()

Is response invalid?

bool
isInformational()

Is response informative?

bool
isSuccessful()

Is response successful?

bool
isRedirection()

Is the response a redirect?

bool
isClientError()

Is there a client error?

bool
isServerError()

Was there a server side error?

bool
isOk()

Is the response OK?

bool
isForbidden()

Is the response forbidden?

bool
isNotFound()

Is the response a not found error?

bool
isRedirect(string $location = null)

Is the response a redirect of some form?

bool
isEmpty()

Is the response empty?

static 
closeOutputBuffers(int $targetLevel, bool $flush)

Cleans or flushes output buffers up to target level.

ensureIEOverSSLCompatibility(Request $request)

Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.

Details

__construct($content = '', int $status = 200, array $headers = array())

Parameters

$content
int $status
array $headers

Exceptions

InvalidArgumentException When the HTTP status code is not valid

static Response create(mixed $content = '', int $status = 200, array $headers = array())

Factory method for chainability.

Example:

return Response::create($body, 200)
    ->setSharedMaxAge(300);

Parameters

mixed $content The response content, see setContent()
int $status The response status code
array $headers An array of response headers

Return Value

Response

string __toString()

Returns the Response as an HTTP string.

The string representation of the Response is the same as the one that will be sent to the client only if the prepare() method has been called before.

Return Value

string The Response as an HTTP string

See also

__clone()

Clones the current Response instance.

$this prepare(Request $request)

Prepares the Response before it is sent to the client.

This method tweaks the Response to ensure that it is compliant with RFC 2616. Most of the changes are based on the Request that is "associated" with this Response.

Parameters

Request $request

Return Value

$this

$this sendHeaders()

Sends HTTP headers.

Return Value

$this

$this sendContent()

Sends content for the current web response.

Return Value

$this

$this send()

Sends HTTP headers and content.

Return Value

$this

$this setContent(mixed $content)

Sets the response content.

Valid types are strings, numbers, null, and objects that implement a __toString() method.

Parameters

mixed $content Content that can be cast to string

Return Value

$this

Exceptions

UnexpectedValueException

string getContent()

Gets the current response content.

Return Value

string Content

$this setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

Parameters

string $version

Return Value

$this

string getProtocolVersion()

Gets the HTTP protocol version.

Return Value

string

$this setStatusCode(int $code, $text = null)

Sets the response status code.

If the status text is null it will be automatically populated for the known status codes and left empty otherwise.

Parameters

int $code
$text

Return Value

$this

Exceptions

InvalidArgumentException When the HTTP status code is not valid

int getStatusCode()

Retrieves the status code for the current web response.

Return Value

int

$this setCharset(string $charset)

Sets the response charset.

Parameters

string $charset

Return Value

$this

string|null getCharset()

Retrieves the response charset.

Return Value

string|null

bool isCacheable()

Returns true if the response may safely be kept in a shared (surrogate) cache.

Responses marked "private" with an explicit Cache-Control directive are considered uncacheable.

Responses with neither a freshness lifetime (Expires, max-age) nor cache validator (Last-Modified, ETag) are considered uncacheable because there is no way to tell when or how to remove them from the cache.

Note that RFC 7231 and RFC 7234 possibly allow for a more permissive implementation, for example "status codes that are defined as cacheable by default [...] can be reused by a cache with heuristic expiration unless otherwise indicated" (https://tools.ietf.org/html/rfc7231#section-6.1)

Return Value

bool

bool isFresh()

Returns true if the response is "fresh".

Fresh responses may be served from cache without any interaction with the origin. A response is considered fresh when it includes a Cache-Control/max-age indicator or Expires header and the calculated age is less than the freshness lifetime.

Return Value

bool

bool isValidateable()

Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request.

Return Value

bool

$this setPrivate()

Marks the response as "private".

It makes the response ineligible for serving other clients.

Return Value

$this

$this setPublic()

Marks the response as "public".

It makes the response eligible for serving other clients.

Return Value

$this

$this setImmutable(bool $immutable = true)

Marks the response as "immutable".

Parameters

bool $immutable

Return Value

$this

bool isImmutable()

Returns true if the response is marked as "immutable".

Return Value

bool

bool mustRevalidate()

Returns true if the response must be revalidated by caches.

This method indicates that the response must not be served stale by a cache in any circumstance without first revalidating with the origin. When present, the TTL of the response should not be overridden to be greater than the value provided by the origin.

Return Value

bool

DateTimeInterface|null getDate()

Returns the Date header as a DateTime instance.

Return Value

DateTimeInterface|null

Exceptions

RuntimeException When the header is not parseable

$this setDate(DateTimeInterface $date)

Sets the Date header.

Parameters

DateTimeInterface $date

Return Value

$this

int getAge()

Returns the age of the response in seconds.

Return Value

int

$this expire()

Marks the response stale by setting the Age header to be equal to the maximum age of the response.

Return Value

$this

DateTimeInterface|null getExpires()

Returns the value of the Expires header as a DateTime instance.

Return Value

DateTimeInterface|null

$this setExpires(DateTimeInterface $date = null)

Sets the Expires HTTP header with a DateTime instance.

Passing null as value will remove the header.

Parameters

DateTimeInterface $date

Return Value

$this

int|null getMaxAge()

Returns the number of seconds after the time specified in the response's Date header when the response should no longer be considered fresh.

First, it checks for a s-maxage directive, then a max-age directive, and then it falls back on an expires header. It returns null when no maximum age can be established.

Return Value

int|null

$this setMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh.

This methods sets the Cache-Control max-age directive.

Parameters

int $value

Return Value

$this

$this setSharedMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh by shared caches.

This methods sets the Cache-Control s-maxage directive.

Parameters

int $value

Return Value

$this

int|null getTtl()

Returns the response's time-to-live in seconds.

It returns null when no freshness information is present in the response.

When the responses TTL is <= 0, the response may not be served from cache without first revalidating with the origin.

Return Value

int|null

$this setTtl(int $seconds)

Sets the response's time-to-live for shared caches in seconds.

This method adjusts the Cache-Control/s-maxage directive.

Parameters

int $seconds

Return Value

$this

$this setClientTtl(int $seconds)

Sets the response's time-to-live for private/client caches in seconds.

This method adjusts the Cache-Control/max-age directive.

Parameters

int $seconds

Return Value

$this

DateTimeInterface|null getLastModified()

Returns the Last-Modified HTTP header as a DateTime instance.

Return Value

DateTimeInterface|null

Exceptions

RuntimeException When the HTTP header is not parseable

$this setLastModified(DateTimeInterface $date = null)

Sets the Last-Modified HTTP header with a DateTime instance.

Passing null as value will remove the header.

Parameters

DateTimeInterface $date

Return Value

$this

string|null getEtag()

Returns the literal value of the ETag HTTP header.

Return Value

string|null

$this setEtag(string $etag = null, bool $weak = false)

Sets the ETag value.

Parameters

string $etag The ETag unique identifier or null to remove the header
bool $weak Whether you want a weak ETag or not

Return Value

$this

$this setCache(array $options)

Sets the response's cache headers (validation and/or expiration).

Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable.

Parameters

array $options

Return Value

$this

Exceptions

InvalidArgumentException

$this setNotModified()

Modifies the response so that it conforms to the rules defined for a 304 status code.

This sets the status, removes the body, and discards any headers that MUST NOT be included in 304 responses.

Return Value

$this

See also

bool hasVary()

Returns true if the response includes a Vary header.

Return Value

bool

array getVary()

Returns an array of header names given in the Vary header.

Return Value

array

$this setVary(string|array $headers, bool $replace = true)

Sets the Vary header.

Parameters

string|array $headers
bool $replace Whether to replace the actual value or not (true by default)

Return Value

$this

bool isNotModified(Request $request)

Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request.

If the Response is not modified, it sets the status code to 304 and removes the actual content by calling the setNotModified() method.

Parameters

Request $request

Return Value

bool true if the Response validators match the Request, false otherwise

bool isInvalid()

Is response invalid?

Return Value

bool

See also

bool isInformational()

Is response informative?

Return Value

bool

bool isSuccessful()

Is response successful?

Return Value

bool

bool isRedirection()

Is the response a redirect?

Return Value

bool

bool isClientError()

Is there a client error?

Return Value

bool

bool isServerError()

Was there a server side error?

Return Value

bool

bool isOk()

Is the response OK?

Return Value

bool

bool isForbidden()

Is the response forbidden?

Return Value

bool

bool isNotFound()

Is the response a not found error?

Return Value

bool

bool isRedirect(string $location = null)

Is the response a redirect of some form?

Parameters

string $location

Return Value

bool

bool isEmpty()

Is the response empty?

Return Value

bool

static closeOutputBuffers(int $targetLevel, bool $flush)

Cleans or flushes output buffers up to target level.

Resulting level can be greater than target level if a non-removable buffer has been encountered.

Parameters

int $targetLevel
bool $flush

protected ensureIEOverSSLCompatibility(Request $request)

Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.

Parameters

Request $request

See also