function FinishResponseSubscriber::isCacheControlCustomized

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php \Drupal\Core\EventSubscriber\FinishResponseSubscriber::isCacheControlCustomized()
  2. 10 core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php \Drupal\Core\EventSubscriber\FinishResponseSubscriber::isCacheControlCustomized()
  3. 11.x core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php \Drupal\Core\EventSubscriber\FinishResponseSubscriber::isCacheControlCustomized()

Determine whether the given response has a custom Cache-Control header.

Upon construction, the ResponseHeaderBag is initialized with an empty Cache-Control header. Consequently it is not possible to check whether the header was set explicitly by simply checking its presence. Instead, it is necessary to examine the computed Cache-Control header and compare with values known to be present only when Cache-Control was never set explicitly.

When neither Cache-Control nor any of the ETag, Last-Modified, Expires headers are set on the response, ::get('Cache-Control') returns the value 'no-cache, private'. If any of ETag, Last-Modified or Expires are set but not Cache-Control, then 'private, must-revalidate' (in exactly this order) is returned.

Parameters

\Symfony\Component\HttpFoundation\Response $response: The response object.

Return value

bool TRUE when Cache-Control header was set explicitly on the given response.

See also

\Symfony\Component\HttpFoundation\ResponseHeaderBag::computeCacheControlValue()

1 call to FinishResponseSubscriber::isCacheControlCustomized()
FinishResponseSubscriber::onRespond in core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php
Sets extra headers on successful responses.

File

core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php, line 216

Class

FinishResponseSubscriber
Response subscriber to handle finished responses.

Namespace

Drupal\Core\EventSubscriber

Code

protected function isCacheControlCustomized(Response $response) {
    $cache_control = $response->headers
        ->get('Cache-Control');
    return $cache_control != 'no-cache, private' && $cache_control != 'private, must-revalidate';
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.