Same name and namespace in other branches
  1. 8.9.x core/modules/rest/src/Plugin/ResourceBase.php \Drupal\rest\Plugin\ResourceBase::routes()
  2. 9 core/modules/rest/src/Plugin/ResourceBase.php \Drupal\rest\Plugin\ResourceBase::routes()

Returns a collection of routes with URL path information for the resource.

This method determines where a resource is reachable, what path replacements are used, the required HTTP method for the operation etc.

Return value

\Symfony\Component\Routing\RouteCollection A collection of routes that should be registered for this resource.

Overrides ResourceInterface::routes

File

core/modules/rest/src/Plugin/ResourceBase.php, line 98

Class

ResourceBase
Common base class for resource plugins.

Namespace

Drupal\rest\Plugin

Code

public function routes() {
  $collection = new RouteCollection();
  $definition = $this
    ->getPluginDefinition();
  $canonical_path = $definition['uri_paths']['canonical'] ?? '/' . strtr($this->pluginId, ':', '/') . '/{id}';
  $create_path = $definition['uri_paths']['create'] ?? '/' . strtr($this->pluginId, ':', '/');
  $route_name = strtr($this->pluginId, ':', '.');
  $methods = $this
    ->availableMethods();
  foreach ($methods as $method) {
    $path = $method === 'POST' ? $create_path : $canonical_path;
    $route = $this
      ->getBaseRoute($path, $method);

    // Note that '_format' and '_content_type_format' route requirements are
    // added in ResourceRoutes::getRoutesForResourceConfig().
    $collection
      ->add("{$route_name}.{$method}", $route);
  }
  return $collection;
}