Commit 1083e8a
committed
bug #62488 [HttpKernel] Make
This PR was merged into the 6.4 branch.
Discussion
----------
[HttpKernel] Make `#[Cache]` respect all explicit cache directives set in controller
| Q | A
| ------------- | ---
| Branch? | 7.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | Fix -
| License | MIT
**Before:**
```php
#[Cache(maxage: 20, public: true, noStore: false)]
public function index(): Response
{
$response = new Response();
$response->setMaxAge(300);
$response->setPrivate();
$response->headers->addCacheControlDirective('no-store');
return $response;
}
```
The previous controller will result : `Cache-Control : max-age=300, public`
instead of : `Cache-Control : max-age=300, private, no-store`
Controller-defined directives (private, no-store) were lost, which is unexpected and it limits the flexibility of controller-level cache control.
The `#[Cache]` is meant for defining default values and shouldn't override the controller.
Even the `ResponseHeaderBag::computeCacheControlValue()` respects explicit visibility but the `#[Cache]` doesn't
```php
$header = $this->getCacheControlHeader();
if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
return $header;
}
```
**After:**
```php
#[Cache(public: true, maxage: 3600, noStore: false)]
public function index(User $user): Response
{
$response = new Response($content);
if ($user->shouldStoreLonger()) {
$response->setMaxAge(7200);
}
if ($user->shouldNotStore()) {
$response->headers->addCacheControlDirective('no-store');
}
if ($user->shouldBePrivate()) {
$response->setPrivate();
}
// Otherwise, `#[Cache]` should take over if no condition was met
return $response;
}
```
> [!NOTE]
> This PR does not change the default behavior of computed cache headers. The `#[Cache]` attribute can still override the default values, only explicitly defined controller directives take precedence.
Commits
-------
a6053f5 [HttpKernel] Make `#[Cache]` respect all explicit cache directives set in controller#[Cache] respect all explicit cache directives set in controller (ayyoub-afwallah)File tree
2 files changed
+45
-7
lines changed- src/Symfony/Component/HttpKernel
- EventListener
- Tests/EventListener
2 files changed
+45
-7
lines changedLines changed: 21 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
129 | 141 | | |
130 | 142 | | |
131 | | - | |
| 143 | + | |
132 | 144 | | |
133 | 145 | | |
134 | 146 | | |
135 | 147 | | |
136 | 148 | | |
137 | 149 | | |
138 | 150 | | |
139 | | - | |
| 151 | + | |
140 | 152 | | |
141 | 153 | | |
142 | 154 | | |
143 | | - | |
| 155 | + | |
144 | 156 | | |
145 | 157 | | |
146 | 158 | | |
147 | | - | |
| 159 | + | |
148 | 160 | | |
149 | 161 | | |
150 | 162 | | |
151 | | - | |
| 163 | + | |
152 | 164 | | |
153 | 165 | | |
154 | 166 | | |
| |||
161 | 173 | | |
162 | 174 | | |
163 | 175 | | |
| 176 | + | |
| 177 | + | |
164 | 178 | | |
165 | | - | |
| 179 | + | |
166 | 180 | | |
167 | 181 | | |
168 | 182 | | |
169 | | - | |
| 183 | + | |
170 | 184 | | |
171 | 185 | | |
172 | 186 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
339 | 363 | | |
340 | 364 | | |
341 | 365 | | |
| |||
0 commit comments