JsonResponse
class JsonResponse extends Response
Response represents an HTTP response in JSON format.
Note that this class does not force the returned JSON content to be an object. It is however recommended that you do return an object as it protects yourself against XSSI and JSON-JavaScript Hijacking.
Constants
Properties
ResponseHeaderBag | $headers | from Response | |
protected string | $content | from Response | |
protected string | $version | from Response | |
protected int | $statusCode | from Response | |
protected string | $statusText | from Response | |
protected string | $charset | from Response | |
static array | $statusTexts | Status codes translation table. | from Response |
protected | $data | ||
protected | $callback | ||
protected | $encodingOptions |
Methods
No description
Factory method for chainability.
Sets the HTTP protocol version (1.0 or 1.1).
Returns true if the response may safely be kept in a shared (surrogate) cache.
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 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.
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.
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.
Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request.
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.
Make easier the creation of JsonResponse from raw json.
Sets the JSONP callback.
Sets a raw string containing a JSON document to be sent.
Sets the data to be sent as JSON.
Returns options used while encoding data to JSON.
Sets options used while encoding data to JSON.
Updates the content and headers according to the JSON data and callback.
Details
static Response
create(mixed $data = null, int $status = 200, array $headers = array())
Factory method for chainability.
Example:
return JsonResponse::create($data, 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.