Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. OptionsRequestSubscriber.php

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

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal