@@ -24,10 +24,25 @@ Expiration with the ``Cache-Control`` Header
2424Most of the time, you will use the ``Cache-Control `` header, which
2525is used to specify many different cache directives::
2626
27- // sets the number of seconds after which the response
28- // should no longer be considered fresh by shared caches
29- $response->setPublic();
30- $response->setMaxAge(600);
27+ .. configuration-block ::
28+
29+ .. code-block :: php-attributes
30+
31+ use Symfony\Component\HttpKernel\Attribute\Cache;
32+ // ...
33+
34+ #[Cache(public: true, maxage: 600)]
35+ public function index()
36+ {
37+ // ...
38+ }
39+
40+ .. code-block :: php
41+
42+ // sets the number of seconds after which the response
43+ // should no longer be considered fresh by shared caches
44+ $response->setPublic();
45+ $response->setMaxAge(600);
3146
3247 The ``Cache-Control `` header would take on the following format (it may have
3348additional directives):
@@ -57,13 +72,28 @@ or disadvantage to either.
5772
5873According to the HTTP specification, "the ``Expires `` header field gives
5974the date/time after which the response is considered stale." The ``Expires ``
60- header can be set with the ``setExpires() `` ``Response `` method. It takes a
61- ``DateTime `` instance as an argument::
75+ header can be set with the ``expires `` option of the ``#[Cache()] `` attribute or
76+ the ``setExpires() `` ``Response `` method::
77+
78+ .. configuration-block ::
79+
80+ .. code-block :: php-attributes
81+
82+ use Symfony\Component\HttpKernel\Attribute\Cache;
83+ // ...
84+
85+ #[Cache(expires: '+600 seconds')]
86+ public function index()
87+ {
88+ // ...
89+ }
90+
91+ .. code-block :: php
6292
63- $date = new DateTime();
64- $date->modify('+600 seconds');
93+ $date = new DateTime();
94+ $date->modify('+600 seconds');
6595
66- $response->setExpires($date);
96+ $response->setExpires($date);
6797
6898 The resulting HTTP header will look like this:
6999
@@ -73,8 +103,8 @@ The resulting HTTP header will look like this:
73103
74104 .. note ::
75105
76- The ``setExpires() `` method automatically converts the date to the GMT
77- timezone as required by the specification.
106+ The ``expires` option and the `` setExpires() `` method automatically convert
107+ the date to the GMT timezone as required by the specification.
78108
79109Note that in HTTP versions before 1.1 the origin server wasn't required to
80110send the ``Date `` header. Consequently, the cache (e.g. the browser) might
0 commit comments