HTTP_CONTINUE
HTTP_CONTINUE = 100
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.
HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = ['must_revalidate' => false, 'no_cache' => false, 'no_store' => false, 'no_transform' => false, 'public' => false, 'private' => false, 'proxy_revalidate' => false, 'max_age' => true, 's_maxage' => true, 'immutable' => false, 'last_modified' => true, 'etag' => true]
$headers : \Symfony\Component\HttpFoundation\ResponseHeaderBag
$statusTexts : array
Status codes translation table.
The list of codes is complete according to the {@link https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml} (last updated 2021-10-01).
Unless otherwise noted, the status code is defined in RFC2616.
$content : string
$version : string
$statusCode : int
$statusText : string
$charset : string
create(mixed $data = null, int $status = 200, array $headers = []) : static
Factory method for chainability.
Example:
return JsonResponse::create(['key' => 'value']) ->setSharedMaxAge(300);
mixed | $data | The JSON response data |
int | $status | The response status code |
array | $headers | An array of response headers |
__toString() : string
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.
None found |
None found |
prepare(\Symfony\Component\HttpFoundation\Request $request) : $this
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.
\Symfony\Component\HttpFoundation\Request | $request |
None found |
None found |
None found |
None found |
setContent(?string $content) : $this
Sets the response content.
?string | $content |
None found |
None found |
setProtocolVersion(string $version) : $this
Sets the HTTP protocol version (1.0 or 1.1).
string | $version |
final |
---|
final |
---|
setStatusCode(int $code, string $text = null) : $this
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.
int | $code | |
string | $text |
When the HTTP status code is not valid
final |
---|
getStatusCode() : int
Retrieves the status code for the current web response.
final |
---|
setCharset(string $charset) : $this
Sets the response charset.
string | $charset |
final |
---|
final |
---|
isCacheable() : bool
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)
final |
---|
isFresh() : bool
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.
final |
---|
isValidateable() : bool
Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request.
final |
---|
setPrivate() : $this
Marks the response as "private".
It makes the response ineligible for serving other clients.
final |
---|
setPublic() : $this
Marks the response as "public".
It makes the response eligible for serving other clients.
final |
---|
setImmutable(bool $immutable = true) : $this
Marks the response as "immutable".
bool | $immutable |
final |
---|
isImmutable() : bool
Returns true if the response is marked as "immutable".
final |
---|
mustRevalidate() : bool
Returns true if the response must be revalidated by shared caches once it has become stale.
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.
final |
---|
getDate() : ?\DateTimeInterface
Returns the Date header as a DateTime instance.
When the header is not parseable
final |
---|
setDate(\DateTimeInterface $date) : $this
Sets the Date header.
\DateTimeInterface | $date |
final |
---|
final |
---|
expire() : $this
Marks the response stale by setting the Age header to be equal to the maximum age of the response.
None found |
getExpires() : ?\DateTimeInterface
Returns the value of the Expires header as a DateTime instance.
final |
---|
setExpires(\DateTimeInterface $date = null) : $this
Sets the Expires HTTP header with a DateTime instance.
Passing null as value will remove the header.
\DateTimeInterface | $date |
final |
---|
getMaxAge() : ?int
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.
final |
---|
setMaxAge(int $value) : $this
Sets the number of seconds after which the response should no longer be considered fresh.
This methods sets the Cache-Control max-age directive.
int | $value |
final |
---|
setSharedMaxAge(int $value) : $this
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 | $value |
final |
---|
getTtl() : ?int
Returns the response's time-to-live in seconds.
It returns null when no freshness information is present in the response.
When the response's TTL is 0, the response may not be served from cache without first revalidating with the origin.
final |
---|
setTtl(int $seconds) : $this
Sets the response's time-to-live for shared caches in seconds.
This method adjusts the Cache-Control/s-maxage directive.
int | $seconds |
final |
---|
setClientTtl(int $seconds) : $this
Sets the response's time-to-live for private/client caches in seconds.
This method adjusts the Cache-Control/max-age directive.
int | $seconds |
final |
---|
getLastModified() : ?\DateTimeInterface
Returns the Last-Modified HTTP header as a DateTime instance.
When the HTTP header is not parseable
final |
---|
setLastModified(\DateTimeInterface $date = null) : $this
Sets the Last-Modified HTTP header with a DateTime instance.
Passing null as value will remove the header.
\DateTimeInterface | $date |
final |
---|
final |
---|
setEtag(string|null $etag = null, bool $weak = false) : $this
Sets the ETag value.
string|null | $etag | The ETag unique identifier or null to remove the header |
bool | $weak | Whether you want a weak ETag or not |
final |
---|
setCache(array $options) : $this
Sets the response's cache headers (validation and/or expiration).
Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.
array | $options |
final |
---|
setNotModified() : $this
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.
final |
---|
final |
---|
getVary() : array
Returns an array of header names given in the Vary header.
final |
---|
setVary(string|array $headers, bool $replace = true) : $this
Sets the Vary header.
string|array | $headers | |
bool | $replace | Whether to replace the actual value or not (true by default) |
final |
---|
isNotModified(\Symfony\Component\HttpFoundation\Request $request) : bool
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.
\Symfony\Component\HttpFoundation\Request | $request |
final |
---|
final |
---|
final |
---|
final |
---|
final |
---|
final |
---|
final |
---|
final |
---|
final |
---|
final |
---|
isRedirect(string $location = null) : bool
Is the response a redirect of some form?
string | $location |
final |
---|
final |
---|
closeOutputBuffers(int $targetLevel, bool $flush) : void
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.
int | $targetLevel | |
bool | $flush |
final |
---|
setContentSafe(bool $safe = true) : void
Marks a response as safe according to RFC8674.
bool | $safe |
None found |
fromJsonString(string $data, int $status = 200, array $headers = []) : static
Factory method for chainability.
Example:
return JsonResponse::fromJsonString('{"key": "value"}') ->setSharedMaxAge(300);
string | $data | The JSON response string |
int | $status | The response status code |
array | $headers | An array of response headers |
None found |
setCallback(string|null $callback = null) : $this
Sets the JSONP callback.
string|null | $callback | The JSONP callback or null to use none |
When the callback name is not valid
None found |
setJson(string $json) : $this
Sets a raw string containing a JSON document to be sent.
string | $json |
None found |
setData(mixed $data = []) : $this
Sets the data to be sent as JSON.
mixed | $data |
None found |
getEncodingOptions() : int
Returns options used while encoding data to JSON.
None found |
setEncodingOptions(int $encodingOptions) : $this
Sets options used while encoding data to JSON.
int | $encodingOptions |
None found |
ensureIEOverSSLCompatibility(\Symfony\Component\HttpFoundation\Request $request) : void
Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9.
\Symfony\Component\HttpFoundation\Request | $request |
final |
---|
update() : $this
Updates the content and headers according to the JSON data and callback.
None found |