Response
class Response
Response represents an HTTP response.
Constants
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
No description
Factory method for chainability.
Returns the Response as an HTTP string.
Clones the current Response instance.
Sends HTTP headers.
Sends content for the current web response.
Sends HTTP headers and content.
Sets the response content.
Gets the current response content.
Sets the HTTP protocol version (1.0 or 1.1).
Gets the HTTP protocol version.
Sets the response status code.
Retrieves the status code for the current web response.
Sets the response charset.
Retrieves the response charset.
Returns true if the response may safely be kept in a shared (surrogate) cache.
Returns true if the response is "fresh".
Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request.
Marks the response as "private".
Marks the response as "public".
Marks the response as "immutable".
Returns true if the response is marked as "immutable".
Returns true if the response must be revalidated by caches.
Returns the Date header as a DateTime instance.
Returns the age of the response in seconds.
Marks the response stale by setting the Age header to be equal to the maximum age of the response.
Returns the value of the Expires header as a DateTime instance.
Returns the number of seconds after the time specified in the response's Date header when the response should no longer be considered fresh.
Sets the number of seconds after which the response should no longer be considered fresh.
Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
Returns the response's time-to-live in seconds.
Sets the response's time-to-live for shared caches in seconds.
Sets the response's time-to-live for private/client caches in seconds.
Returns the Last-Modified HTTP header as a DateTime instance.
Sets the Last-Modified HTTP header with a DateTime instance.
Returns the literal value of the ETag HTTP header.
Sets the ETag value.
Sets the response's cache headers (validation and/or expiration).
Modifies the response so that it conforms to the rules defined for a 304 status code.
Returns true if the response includes a Vary header.
Returns an array of header names given in the Vary header.
Sets the Vary header.
Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request.
Is response invalid?
Is response informative?
Is response successful?
Is the response a redirect?
Is there a client error?
Was there a server side error?
Is the response OK?
Is the response forbidden?
Is the response a not found error?
Is the response a redirect of some form?
Is the response empty?
Cleans or flushes output buffers up to target level.
Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.
Details
static Response
create(mixed $content = '', int $status = 200, array $headers = array())
Factory method for chainability.
Example:
return Response::create($body, 200)
->setSharedMaxAge(300);
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.
$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.
$this
setContent(mixed $content)
Sets the response content.
Valid types are strings, numbers, null, and objects that implement a __toString() method.
$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.
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)
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.
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".
It makes the response ineligible for serving other clients.
$this
setPublic()
Marks the response as "public".
It makes the response eligible for serving other clients.
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.
$this
expire()
Marks the response stale by setting the Age header to be equal to the maximum age of the response.
$this
setExpires(DateTimeInterface $date = null)
Sets the Expires HTTP header with a DateTime instance.
Passing null as value will remove the header.
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.
$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.
$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.
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.
$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.
$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.
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.
Passing null as value will remove the header.
$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.
$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.
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.
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.
protected
ensureIEOverSSLCompatibility(Request $request)
Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.