class OptionsRequestSubscriber

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php \Drupal\Core\EventSubscriber\OptionsRequestSubscriber
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php \Drupal\Core\EventSubscriber\OptionsRequestSubscriber
  3. 10 core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php \Drupal\Core\EventSubscriber\OptionsRequestSubscriber

Handles options requests.

Listens to KernelEvents::REQUEST and responds to OPTIONS requests by providing an Allow header listing all the HTTP methods allowed for the requested routes.

Hierarchy

  • class \Drupal\Core\EventSubscriber\OptionsRequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OptionsRequestSubscriber

1 file declares its use of OptionsRequestSubscriber
OptionsRequestSubscriberTest.php in core/tests/Drupal/Tests/Core/EventSubscriber/OptionsRequestSubscriberTest.php

File

core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php, line 19

Namespace

Drupal\Core\EventSubscriber
View source
class OptionsRequestSubscriber implements EventSubscriberInterface {
    
    /**
     * The route provider.
     *
     * @var \Drupal\Core\Routing\RouteProviderInterface
     */
    protected $routeProvider;
    
    /**
     * Creates a new OptionsRequestSubscriber instance.
     *
     * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
     *   The route provider.
     */
    public function __construct(RouteProviderInterface $route_provider) {
        $this->routeProvider = $route_provider;
    }
    
    /**
     * Tries to handle the options request.
     *
     * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
     *   The request event.
     */
    public function onRequest(RequestEvent $event) {
        if ($event->getRequest()
            ->isMethod('OPTIONS')) {
            $routes = $this->routeProvider
                ->getRouteCollectionForRequest($event->getRequest());
            // In case we don't have any routes, a 403 should be thrown by the normal
            // request handling.
            if (count($routes) > 0) {
                // Flatten and unique the available methods.
                $methods = array_reduce($routes->all(), function ($methods, Route $route) {
                    return array_merge($methods, $route->getMethods());
                }, []);
                $methods = array_unique($methods);
                $response = new Response('', 200, [
                    'Allow' => implode(', ', $methods),
                ]);
                $event->setResponse($response);
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // Set a high priority so it is executed before routing.
        $events[KernelEvents::REQUEST][] = [
            'onRequest',
            1000,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
OptionsRequestSubscriber::$routeProvider protected property The route provider.
OptionsRequestSubscriber::getSubscribedEvents public static function
OptionsRequestSubscriber::onRequest public function Tries to handle the options request.
OptionsRequestSubscriber::__construct public function Creates a new OptionsRequestSubscriber instance.

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