You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.
Varying the Response for HTTP Cache
Varying the Response for HTTP Cache¶
So far, it's been assumed that each URI has exactly one representation of the target resource. By default, HTTP caching is done by using the URI of the resource as the cache key. If two people request the same URI of a cacheable resource, the second person will receive the cached version.
Sometimes this isn't enough and different versions of the same URI need to
be cached based on one or more request header values. For instance, if you
compress pages when the client supports it, any given URI has two representations:
one when the client supports compression, and one when it does not. This
determination is done by the value of the
Accept-Encoding request header.
In this case, you need the cache to store both a compressed and uncompressed
version of the response for the particular URI and return them based on the
Accept-Encoding value. This is done by using the
header, which is a comma-separated list of different headers whose values
trigger a different representation of the requested resource:
Vary: Accept-Encoding, User-Agent
Vary header would cache different versions of each
resource based on the URI and the value of the
User-Agent request header.
Response object offers a clean interface for managing the
1 2 3 4 5
// sets one vary header $response->setVary('Accept-Encoding'); // sets multiple vary headers $response->setVary(['Accept-Encoding', 'User-Agent']);
setVary() method takes a header name or an array of header names for
which the response varies.
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.