function RouteAccessResponseSubscriber::onRespond

Same name in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php \Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber::onRespond()
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php \Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber::onRespond()
  3. 10 core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php \Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber::onRespond()

Bubbles the route's access result' cacheability metadata.

Parameters

\Symfony\Component\HttpKernel\Event\ResponseEvent $event: The event to process.

File

core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php, line 32

Class

RouteAccessResponseSubscriber
Response subscriber to bubble the route's access result's cacheability.

Namespace

Drupal\Core\EventSubscriber

Code

public function onRespond(ResponseEvent $event) {
    if (!$event->isMainRequest()) {
        return;
    }
    $response = $event->getResponse();
    if (!$response instanceof CacheableResponseInterface) {
        return;
    }
    $request = $event->getRequest();
    // It is possible that route access checking did not occur, for example,
    // when an exception was thrown during route matching. This could happen in
    // an implementation of \Drupal\Core\Routing\EnhancerInterface.
    // @see \Drupal\jsonapi\Revisions\ResourceVersionRouteEnhancer::enhance()
    if ($request->attributes
        ->has(AccessAwareRouterInterface::ACCESS_RESULT)) {
        $response->addCacheableDependency($request->attributes
            ->get(AccessAwareRouterInterface::ACCESS_RESULT));
    }
}

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