You are here

public function ResourceBase::routes in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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
Contains \Drupal\rest\Plugin\ResourceBase.

Class

ResourceBase
Common base class for resource plugins.

Namespace

Drupal\rest\Plugin

Code

public function routes() {
  $collection = new RouteCollection();
  $definition = $this
    ->getPluginDefinition();
  $canonical_path = isset($definition['uri_paths']['canonical']) ? $definition['uri_paths']['canonical'] : '/' . strtr($this->pluginId, ':', '/') . '/{id}';
  $create_path = isset($definition['uri_paths']['https://www.drupal.org/link-relations/create']) ? $definition['uri_paths']['https://www.drupal.org/link-relations/create'] : '/' . strtr($this->pluginId, ':', '/');
  $route_name = strtr($this->pluginId, ':', '.');
  $methods = $this
    ->availableMethods();
  foreach ($methods as $method) {
    $route = $this
      ->getBaseRoute($canonical_path, $method);
    switch ($method) {
      case 'POST':
        $route
          ->setPath($create_path);

        // Restrict the incoming HTTP Content-type header to the known
        // serialization formats.
        $route
          ->addRequirements(array(
          '_content_type_format' => implode('|', $this->serializerFormats),
        ));
        $collection
          ->add("{$route_name}.{$method}", $route);
        break;
      case 'PATCH':

        // Restrict the incoming HTTP Content-type header to the known
        // serialization formats.
        $route
          ->addRequirements(array(
          '_content_type_format' => implode('|', $this->serializerFormats),
        ));
        $collection
          ->add("{$route_name}.{$method}", $route);
        break;
      case 'GET':
      case 'HEAD':

        // Restrict GET and HEAD requests to the media type specified in the
        // HTTP Accept headers.
        foreach ($this->serializerFormats as $format_name) {

          // Expose one route per available format.
          $format_route = clone $route;
          $format_route
            ->addRequirements(array(
            '_format' => $format_name,
          ));
          $collection
            ->add("{$route_name}.{$method}.{$format_name}", $format_route);
        }
        break;
      default:
        $collection
          ->add("{$route_name}.{$method}", $route);
        break;
    }
  }
  return $collection;
}