Class

Symfony\Component\HttpFoundation\Response

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.

Response prepare(Request $request)

Prepares the Response before it is sent to the client.

Response sendHeaders()

Sends HTTP headers.

Response sendContent()

Sends content for the current web response.

Response send()

Sends HTTP headers and content.

Response setContent(mixed $content)

Sets the response content.

string getContent()

Gets the current response content.

Response setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

string getProtocolVersion()

Gets the HTTP protocol version.

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

Sets the response status code.

integer getStatusCode()

Retrieves the status code for the current web response.

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.

Response setPrivate()

Marks the response as "private".

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

Response setDate(DateTime $date)

Sets the Date header.

integer getAge()

Returns the age of the response.

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.

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

Response setMaxAge(integer $value)

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

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

Response setTtl(integer $seconds)

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

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

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

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

Sets the ETag value.

Response setCache(array $options)

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

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

Response 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 138
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

Exceptions

InvalidArgumentException When the HTTP status code is not valid

at line 163
static public 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 179
public 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 190
public __clone()

Clones the current Response instance.

at line 206
public 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 266
public Response sendHeaders()

Sends HTTP headers.

Return Value

Response

at line 296
public Response sendContent()

Sends content for the current web response.

Return Value

Response

at line 310
public Response send()

Sends HTTP headers and content.

Return Value

Response

at line 355
public 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 373
public string getContent()

Gets the current response content.

Return Value

string Content

at line 387
public 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 401
public string getProtocolVersion()

Gets the HTTP protocol version.

Return Value

string The HTTP protocol version

at line 421
public 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 452
public integer getStatusCode()

Retrieves the status code for the current web response.

Return Value

integer Status code

at line 466
public Response setCharset(string $charset)

Sets the response charset.

Parameters

string $charset Character set

Return Value

Response

at line 480
public string getCharset()

Retrieves the response charset.

Return Value

string Character set

at line 498
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 522
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 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 535
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 549
public Response setPrivate()

Marks the response as "private".

It makes the response ineligible for serving other clients.

Return Value

Response

at line 566
public Response setPublic()

Marks the response as "public".

It makes the response eligible for serving other clients.

Return Value

Response

at line 586
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 600
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 614
public Response setDate(DateTime $date)

Sets the Date header.

Parameters

DateTime $date A \DateTime instance

Return Value

Response

at line 627
public integer getAge()

Returns the age of the response.

Return Value

integer The age of the response in seconds

at line 643
public 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 659
public 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 680
public 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 704
public 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 732
public 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 750
public 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 770
public 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 790
public 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 808
public 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 824
public 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 840
public 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 860
public 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 875
public 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 903
public 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 956
public 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 976
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 988
public array getVary()

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

Return Value

array An array of Vary names

at line 1007
public 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 1027
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 1056
public Boolean isInvalid()

Is response invalid?

Return Value

Boolean

at line 1068
public Boolean isInformational()

Is response informative?

Return Value

Boolean

at line 1080
public Boolean isSuccessful()

Is response successful?

Return Value

Boolean

at line 1092
public Boolean isRedirection()

Is the response a redirect?

Return Value

Boolean

at line 1104
public Boolean isClientError()

Is there a client error?

Return Value

Boolean

at line 1116
public Boolean isServerError()

Was there a server side error?

Return Value

Boolean

at line 1128
public Boolean isOk()

Is the response OK?

Return Value

Boolean

at line 1140
public Boolean isForbidden()

Is the response forbidden?

Return Value

Boolean

at line 1152
public Boolean isNotFound()

Is the response a not found error?

Return Value

Boolean

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

Is the response a redirect of some form?

Parameters

string $location

Return Value

Boolean

at line 1178
public Boolean isEmpty()

Is the response empty?

Return Value

Boolean