function OptionsRequestSubscriber::onRequest

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

Tries to handle the options request.

Parameters

\Symfony\Component\HttpKernel\Event\RequestEvent $event: The request event.

File

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

Class

OptionsRequestSubscriber
Handles options requests.

Namespace

Drupal\Core\EventSubscriber

Code

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);
        }
    }
}

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