You are here

class RouteAccessResponseSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php \Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber

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

During routing, access checking is performed. The corresponding access result is stored in the Request object's attributes, just like the matching route object is. In case of a cacheable response, the route's access result also determined the content of the response, and therefore the cacheability of the route's access result should also be applied to the resulting response.

Hierarchy

Expanded class hierarchy of RouteAccessResponseSubscriber

See also

\Drupal\Core\Routing\AccessAwareRouterInterface::ACCESS_RESULT

\Drupal\Core\Routing\AccessAwareRouter::matchRequest()

\Drupal\Core\Routing\AccessAwareRouter::checkAccess()

1 string reference to 'RouteAccessResponseSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses RouteAccessResponseSubscriber
route_access_response_subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber

File

core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php, line 29
Contains \Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class RouteAccessResponseSubscriber implements EventSubscriberInterface {

  /**
   * Bubbles the route's access result' cacheability metadata.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    $response = $event
      ->getResponse();
    if (!$response instanceof CacheableResponseInterface) {
      return;
    }
    $request = $event
      ->getRequest();
    $access_result = $request->attributes
      ->get(AccessAwareRouterInterface::ACCESS_RESULT);
    $response
      ->addCacheableDependency($access_result);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Priority 10, so that it runs before FinishResponseSubscriber, which will
    // expose the cacheability metadata in the form of headers.
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
      10,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteAccessResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
RouteAccessResponseSubscriber::onRespond public function Bubbles the route's access result' cacheability metadata.