class RouteAccessResponseSubscriber

Same name in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php \Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/RouteAccessResponseSubscriber.php \Drupal\Core\EventSubscriber\RouteAccessResponseSubscriber
  3. 10 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()

File

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

Namespace

Drupal\Core\EventSubscriber
View source
class RouteAccessResponseSubscriber implements EventSubscriberInterface {
    
    /**
     * Bubbles the route's access result' cacheability metadata.
     *
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
     *   The event to process.
     */
    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));
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // 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

Title Sort descending Modifiers Object type Summary
RouteAccessResponseSubscriber::getSubscribedEvents public static function
RouteAccessResponseSubscriber::onRespond public function Bubbles the route's access result' cacheability metadata.

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