class Response

Response represents an HTTP response.

Properties

ResponseHeaderBag $headers
static array $statusTexts Status codes translation table.

Methods

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

Constructor.

static Response
create(string $content = '', integer $status = 200, array $headers = array())

Factory method for chainability

string
__toString()

Returns the Response as an HTTP string.

__clone()

Clones the current Response instance.

prepare(Request $request)

Prepares the Response before it is sent to the client.

sendHeaders()

Sends HTTP headers.

sendContent()

Sends content for the current web response.

send()

Sends HTTP headers and content.

setContent(mixed $content)

Sets the response content.

string
getContent()

Gets the current response content.

setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

string
getProtocolVersion()

Gets the HTTP protocol version.

setStatusCode(integer $code, mixed $text = null)

Sets the response status code.

integer
getStatusCode()

Retrieves the status code for the current web response.

setCharset(string $charset)

Sets the response charset.

string
getCharset()

Retrieves the response charset.

Boolean
isCacheable()

Returns true if the response is worth caching under any circumstance.

Boolean
isFresh()

Returns true if the response is "fresh".

Boolean
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.

setPrivate()

Marks the response as "private".

setPublic()

Marks the response as "public".

Boolean
mustRevalidate()

Returns true if the response must be revalidated by caches.

getDate()

Returns the Date header as a DateTime instance.

setDate(DateTime $date)

Sets the Date header.

integer
getAge()

Returns the age of the response.

expire()

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

DateTime|null
getExpires()

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

setExpires(DateTime $date = null)

Sets the Expires HTTP header with a DateTime instance.

integer|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.

setMaxAge(integer $value)

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

setSharedMaxAge(integer $value)

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

integer|null
getTtl()

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

setTtl(integer $seconds)

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

setClientTtl(integer $seconds)

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

DateTime|null
getLastModified()

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

setLastModified(DateTime $date = null)

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

string|null
getEtag()

Returns the literal value of the ETag HTTP header.

setEtag(string|null $etag = null, Boolean $weak = false)

Sets the ETag value.

setCache(array $options)

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

setNotModified()

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

Boolean
hasVary()

Returns true if the response includes a Vary header.

array
getVary()

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

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

Sets the Vary header.

Boolean
isNotModified(Request $request)

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

Boolean
isInvalid()

Is response invalid?

Boolean
isInformational()

Is response informative?

Boolean
isSuccessful()

Is response successful?

Boolean
isRedirection()

Is the response a redirect?

Boolean
isClientError()

Is there a client error?

Boolean
isServerError()

Was there a server side error?

Boolean
isOk()

Is the response OK?

Boolean
isForbidden()

Is the response forbidden?

Boolean
isNotFound()

Is the response a not found error?

Boolean
isRedirect(string $location = null)

Is the response a redirect of some form?

Boolean
isEmpty()

Is the response empty?

Details

at line line 138
__construct(string $content = '', integer $status = 200, array $headers = array())

Constructor.

Parameters

string $content The response content
integer $status The response status code
array $headers An array of response headers

Exceptions

InvalidArgumentException When the HTTP status code is not valid

at line line 163
static Response create(string $content = '', integer $status = 200, array $headers = array())

Factory method for chainability

Example:

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

Parameters

string $content The response content
integer $status The response status code
array $headers An array of response headers

Return Value

Response

at line line 179
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

prepare()

at line line 190
__clone()

Clones the current Response instance.

at line line 206
Response 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 A Request instance

Return Value

Response The current response.

at line line 266
Response sendHeaders()

Sends HTTP headers.

Return Value

Response

at line line 296
Response sendContent()

Sends content for the current web response.

Return Value

Response

at line line 310
Response send()

Sends HTTP headers and content.

Return Value

Response

at line line 355
Response setContent(mixed $content)

Sets the response content.

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

Parameters

mixed $content

Return Value

Response

Exceptions

UnexpectedValueException

at line line 373
string getContent()

Gets the current response content.

Return Value

string Content

at line line 387
Response setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

Parameters

string $version The HTTP protocol version

Return Value

Response

at line line 401
string getProtocolVersion()

Gets the HTTP protocol version.

Return Value

string The HTTP protocol version

at line line 421
Response setStatusCode(integer $code, mixed $text = null)

Sets the response status code.

Parameters

integer $code HTTP status code
mixed $text HTTP status text

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

Return Value

Response

Exceptions

InvalidArgumentException When the HTTP status code is not valid

at line line 452
integer getStatusCode()

Retrieves the status code for the current web response.

Return Value

integer Status code

at line line 466
Response setCharset(string $charset)

Sets the response charset.

Parameters

string $charset Character set

Return Value

Response

at line line 480
string getCharset()

Retrieves the response charset.

Return Value

string Character set

at line line 498
Boolean isCacheable()

Returns true if the response is worth caching under any circumstance.

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.

Return Value

Boolean true if the response is worth caching, false otherwise

at line line 522
Boolean 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

Boolean true if the response is fresh, false otherwise

at line line 535
Boolean 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

Boolean true if the response is validateable, false otherwise

at line line 549
Response setPrivate()

Marks the response as "private".

It makes the response ineligible for serving other clients.

Return Value

Response

at line line 566
Response setPublic()

Marks the response as "public".

It makes the response eligible for serving other clients.

Return Value

Response

at line line 586
Boolean 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

Boolean true if the response must be revalidated by a cache, false otherwise

at line line 600
DateTime getDate()

Returns the Date header as a DateTime instance.

Return Value

DateTime A \DateTime instance

Exceptions

RuntimeException When the header is not parseable

at line line 614
Response setDate(DateTime $date)

Sets the Date header.

Parameters

DateTime $date A \DateTime instance

Return Value

Response

at line line 627
integer getAge()

Returns the age of the response.

Return Value

integer The age of the response in seconds

at line line 643
Response expire()

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

Return Value

Response

at line line 659
DateTime|null getExpires()

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

Return Value

DateTime|null A DateTime instance or null if the header does not exist

at line line 680
Response setExpires(DateTime $date = null)

Sets the Expires HTTP header with a DateTime instance.

Passing null as value will remove the header.

Parameters

DateTime $date A \DateTime instance or null to remove the header

Return Value

Response

at line line 704
integer|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

integer|null Number of seconds

at line line 732
Response setMaxAge(integer $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

integer $value Number of seconds

Return Value

Response

at line line 750
Response setSharedMaxAge(integer $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

integer $value Number of seconds

Return Value

Response

at line line 770
integer|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

integer|null The TTL in seconds

at line line 790
Response setTtl(integer $seconds)

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

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

Parameters

integer $seconds Number of seconds

Return Value

Response

at line line 808
Response setClientTtl(integer $seconds)

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

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

Parameters

integer $seconds Number of seconds

Return Value

Response

at line line 824
DateTime|null getLastModified()

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

Return Value

DateTime|null A DateTime instance or null if the header does not exist

Exceptions

RuntimeException When the HTTP header is not parseable

at line line 840
Response setLastModified(DateTime $date = null)

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

Passing null as value will remove the header.

Parameters

DateTime $date A \DateTime instance or null to remove the header

Return Value

Response

at line line 860
string|null getEtag()

Returns the literal value of the ETag HTTP header.

Return Value

string|null The ETag HTTP header or null if it does not exist

at line line 875
Response setEtag(string|null $etag = null, Boolean $weak = false)

Sets the ETag value.

Parameters

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

Return Value

Response

at line line 903
Response setCache(array $options)

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

Available options are: etag, lastmodified, maxage, s_maxage, private, and public.

Parameters

array $options An array of cache options

Return Value

Response

Exceptions

InvalidArgumentException

at line line 956
Response 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

Response

See also

http://tools.ietf.org/html/rfc2616#section-10.3.5

at line line 976
Boolean hasVary()

Returns true if the response includes a Vary header.

Return Value

Boolean true if the response includes a Vary header, false otherwise

at line line 988
array getVary()

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

Return Value

array An array of Vary names

at line line 1007
Response setVary(string|array $headers, Boolean $replace = true)

Sets the Vary header.

Parameters

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

Return Value

Response

at line line 1027
Boolean 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 A Request instance

Return Value

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

at line line 1056
Boolean isInvalid()

Is response invalid?

Return Value

Boolean

at line line 1068
Boolean isInformational()

Is response informative?

Return Value

Boolean

at line line 1080
Boolean isSuccessful()

Is response successful?

Return Value

Boolean

at line line 1092
Boolean isRedirection()

Is the response a redirect?

Return Value

Boolean

at line line 1104
Boolean isClientError()

Is there a client error?

Return Value

Boolean

at line line 1116
Boolean isServerError()

Was there a server side error?

Return Value

Boolean

at line line 1128
Boolean isOk()

Is the response OK?

Return Value

Boolean

at line line 1140
Boolean isForbidden()

Is the response forbidden?

Return Value

Boolean

at line line 1152
Boolean isNotFound()

Is the response a not found error?

Return Value

Boolean

at line line 1166
Boolean isRedirect(string $location = null)

Is the response a redirect of some form?

Parameters

string $location

Return Value

Boolean

at line line 1178
Boolean isEmpty()

Is the response empty?

Return Value

Boolean