@@ -24,10 +24,25 @@ Expiration with the ``Cache-Control`` Header
24
24
Most of the time, you will use the ``Cache-Control `` header, which
25
25
is used to specify many different cache directives::
26
26
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);
31
46
32
47
The ``Cache-Control `` header would take on the following format (it may have
33
48
additional directives):
@@ -57,13 +72,28 @@ or disadvantage to either.
57
72
58
73
According to the HTTP specification, "the ``Expires `` header field gives
59
74
the 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
62
92
63
- $date = new DateTime();
64
- $date->modify('+600 seconds');
93
+ $date = new DateTime();
94
+ $date->modify('+600 seconds');
65
95
66
- $response->setExpires($date);
96
+ $response->setExpires($date);
67
97
68
98
The resulting HTTP header will look like this:
69
99
@@ -73,8 +103,8 @@ The resulting HTTP header will look like this:
73
103
74
104
.. note ::
75
105
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.
78
108
79
109
Note that in HTTP versions before 1.1 the origin server wasn't required to
80
110
send the ``Date `` header. Consequently, the cache (e.g. the browser) might
0 commit comments