public function ResourceBase::routes in Drupal 8
Same name and namespace in other branches
- 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 99
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, ':', '/');
// BC: the REST module originally created the POST URL for a resource by
// reading the 'https://www.drupal.org/link-relations/create' URI path from
// the plugin annotation. For consistency with entity type definitions, that
// then changed to reading the 'create' URI path. For any REST Resource
// plugins that were using the old mechanism, we continue to support that.
if (!isset($definition['uri_paths']['create']) && isset($definition['uri_paths']['https://www.drupal.org/link-relations/create'])) {
@trigger_error('The "https://www.drupal.org/link-relations/create" string as a RestResource plugin annotation URI path key is deprecated in Drupal 8.4.0, now a valid link relation type name must be specified, so "create" must be specified instead before Drupal 9.0.0. See https://www.drupal.org/node/2737401.', E_USER_DEPRECATED);
$create_path = $definition['uri_paths']['https://www.drupal.org/link-relations/create'];
}
$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);
// BC: the REST module originally created per-format GET routes, instead
// of a single route. To minimize the surface of this BC layer, this uses
// route definitions that are as empty as possible, plus an outbound route
// processor.
// @see \Drupal\rest\RouteProcessor\RestResourceGetRouteProcessorBC
if ($method === 'GET' || $method === 'HEAD') {
foreach ($this->serializerFormats as $format_name) {
$collection
->add("{$route_name}.{$method}.{$format_name}", (new BcRoute())
->setRequirement('_format', $format_name));
}
}
}
return $collection;
}