public function ResourceBase::routes in Drupal 9
Same name and namespace in other branches
- 8 core/modules/rest/src/Plugin/ResourceBase.php \Drupal\rest\Plugin\ResourceBase::routes()
- 10 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\PluginCode
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']['create']) ? $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;
}