Symfony2 API
Class

Symfony\Component\HttpFoundation\Response

class Response

Response represents an HTTP response.

Properties

ResponseHeaderBag $headers
static $statusTexts

Methods

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

Constructor.

string __toString()

Returns the response content as it will be sent (with the headers).

__clone()

Clones the current Response instance.

prepare()

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, string $text = null)

Sets the response status code.

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

DateTime 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 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()

Sets the number of seconds after the time specified in the response's Date header when the 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 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 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 getEtag()

Returns the literal value of the ETag HTTP header.

setEtag(string $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()

Boolean isInformational()

Boolean isSuccessful()

Boolean isRedirection()

Boolean isClientError()

Boolean isServerError()

Boolean isOk()

Boolean isForbidden()

Boolean isNotFound()

Boolean isRedirect(string $location = null)

Boolean isEmpty()

Details

at line 87
public __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

at line 103
public string __toString()

Returns the response content as it will be sent (with the headers).

Return Value

string The response content

at line 116
public __clone()

Clones the current Response instance.

at line 127
public prepare()

Prepares the Response before it is sent to the client.

This method tweaks the Response to ensure that it is
compliant with RFC 2616.

at line 151
public sendHeaders()

Sends HTTP headers.

at line 179
public sendContent()

Sends content for the current web response.

at line 189
public send()

Sends HTTP headers and content.

at line 208
public setContent(mixed $content)

Sets the response content.

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

Parameters

mixed $content

at line 224
public string getContent()

Gets the current response content.

Return Value

string Content

at line 236
public setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

Parameters

string $version The HTTP protocol version

at line 248
public string getProtocolVersion()

Gets the HTTP protocol version.

Return Value

string The HTTP protocol version

at line 263
public setStatusCode(integer $code, string $text = null)

Sets the response status code.

Parameters

integer $code HTTP status code
string $text HTTP status text

Exceptions

InvalidArgumentException When the HTTP status code is not valid

at line 280
public string getStatusCode()

Retrieves the status code for the current web response.

Return Value

string Status code

at line 292
public setCharset(string $charset)

Sets the response charset.

Parameters

string $charset Character set

at line 304
public string getCharset()

Retrieves the response charset.

Return Value

string Character set

at line 322
public 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 346
public 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 Expiration header and the calculated age is less than the freshness lifetime.

Return Value

Boolean true if the response is fresh, false otherwise

at line 359
public 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 371
public setPrivate()

Marks the response as "private".

It makes the response ineligible for serving other clients.

at line 384
public setPublic()

Marks the response as "public".

It makes the response eligible for serving other clients.

at line 402
public 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 416
public 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 428
public setDate(DateTime $date)

Sets the Date header.

Parameters

DateTime $date A \DateTime instance

at line 439
public integer getAge()

Returns the age of the response.

Return Value

integer The age of the response in seconds

at line 453
public expire()

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

at line 467
public DateTime getExpires()

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

Return Value

DateTime A DateTime instance

at line 481
public setExpires(DateTime $date = null)

Sets the Expires HTTP header with a DateTime instance.

If passed a null value, it removes the header.

Parameters

DateTime $date A \DateTime instance

at line 503
public integer|null getMaxAge()

Sets the number of seconds after the time specified in the response's Date header when the 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 529
public 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

at line 543
public 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

at line 561
public integer 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 The TTL in seconds

at line 579
public 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

at line 593
public 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

at line 605
public DateTime getLastModified()

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

Return Value

DateTime A DateTime instance

at line 619
public setLastModified(DateTime $date = null)

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

If passed a null value, it removes the header.

Parameters

DateTime $date A \DateTime instance

at line 637
public string getEtag()

Returns the literal value of the ETag HTTP header.

Return Value

string The ETag HTTP header

at line 650
public setEtag(string $etag = null, Boolean $weak = false)

Sets the ETag value.

Parameters

string $etag The ETag unique identifier
Boolean $weak Whether you want a weak ETag or not

at line 672
public setCache(array $options)

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

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

Parameters

array $options An array of cache options

at line 721
public 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.

See also

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

at line 739
public 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 751
public array getVary()

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

Return Value

array An array of Vary names

at line 768
public 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)

at line 786
public 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 809
public Boolean isInvalid()

Return Value

Boolean

at line 819
public Boolean isInformational()

Return Value

Boolean

at line 829
public Boolean isSuccessful()

Return Value

Boolean

at line 839
public Boolean isRedirection()

Return Value

Boolean

at line 849
public Boolean isClientError()

Return Value

Boolean

at line 859
public Boolean isServerError()

Return Value

Boolean

at line 869
public Boolean isOk()

Return Value

Boolean

at line 879
public Boolean isForbidden()

Return Value

Boolean

at line 889
public Boolean isNotFound()

Return Value

Boolean

at line 901
public Boolean isRedirect(string $location = null)

Parameters

string $location

Return Value

Boolean

at line 911
public Boolean isEmpty()

Return Value

Boolean