public function RouteSubscriber::dynamicRoutes

Same name in this branch
  1. 8.x drupal/core/modules/views/lib/Drupal/views/EventSubscriber/RouteSubscriber.php \Drupal\views\EventSubscriber\RouteSubscriber::dynamicRoutes()
  2. 8.x drupal/core/modules/rest/lib/Drupal/rest/EventSubscriber/RouteSubscriber.php \Drupal\rest\EventSubscriber\RouteSubscriber::dynamicRoutes()

Adds routes to enabled REST resources.

Parameters

\Drupal\Core\Routing\RouteBuildEvent $event: The route building event.

File

drupal/core/modules/rest/lib/Drupal/rest/EventSubscriber/RouteSubscriber.php, line 54

Class

RouteSubscriber
Subscriber for REST-style routes.

Namespace

Drupal\rest\EventSubscriber

Code

public function dynamicRoutes(RouteBuildEvent $event) {
  $collection = $event
    ->getRouteCollection();
  $enabled_resources = $this->config
    ->get('rest.settings')
    ->load()
    ->get('resources');

  // Iterate over all enabled resource plugins.
  foreach ($enabled_resources as $id => $enabled_methods) {
    $plugin = $this->manager
      ->getInstance(array(
      'id' => $id,
    ));
    foreach ($plugin
      ->routes() as $name => $route) {
      $method = $route
        ->getRequirement('_method');

      // Only expose routes where the method is enabled in the configuration.
      if ($method && isset($enabled_methods[$method])) {
        $route
          ->setRequirement('_access_rest_csrf', 'TRUE');

        // If the array of configured format restrictions is empty for a
        // method always add the route.
        if (empty($enabled_methods[$method])) {
          $collection
            ->add("rest.{$name}", $route);
          continue;
        }

        // If there is no format requirement or if it matches the
        // configuration also add the route.
        $format_requirement = $route
          ->getRequirement('_format');
        if (!$format_requirement || isset($enabled_methods[$method][$format_requirement])) {
          $collection
            ->add("rest.{$name}", $route);
        }
      }
    }
  }
}