function AttributeRouteDiscovery::createControllerRouteCollection
Same name and namespace in other branches
- main core/lib/Drupal/Core/Routing/AttributeRouteDiscovery.php \Drupal\Core\Routing\AttributeRouteDiscovery::createControllerRouteCollection()
Creates a route collection from a controller class's attributed methods.
Parameters
\ReflectionClass<object> $class: The reflection object of the class to generate a route collection for.
Return value
\Symfony\Component\Routing\RouteCollection The route collection.
1 call to AttributeRouteDiscovery::createControllerRouteCollection()
- AttributeRouteDiscovery::collectRoutes in core/
lib/ Drupal/ Core/ Routing/ AttributeRouteDiscovery.php - Creates a collection of routes to add to the route builder.
File
-
core/
lib/ Drupal/ Core/ Routing/ AttributeRouteDiscovery.php, line 86
Class
- AttributeRouteDiscovery
- Discovers routes using Symfony's Route attribute.
Namespace
Drupal\Core\RoutingCode
private function createControllerRouteCollection(\ReflectionClass $class) : RouteCollection {
$collection = new RouteCollection();
$globals = $this->getGlobals($class);
$fqcnAlias = FALSE;
if (!$class->hasMethod('__invoke')) {
foreach ($this->getAttributes($class) as $attribute) {
if ($attribute->aliases) {
throw new InvalidArgumentException(\sprintf('Route aliases cannot be used on non-invokable class "%s".', $class->getName()));
}
}
}
foreach ($class->getMethods() as $method) {
$routeNamesBefore = array_keys($collection->all());
foreach ($this->getAttributes($method) as $attribute) {
$controllerName = $class->getName() . '::' . $method->getName();
if ($method->name === '__invoke') {
$fqcnAlias = TRUE;
$controllerName = $class->getName();
}
$attribute->defaults = [
'_controller' => $controllerName,
] + $attribute->defaults;
$this->addRoute($collection, $attribute, $globals, $class, $method);
}
if ($collection->count() - \count($routeNamesBefore) === 1) {
$newRouteName = current(array_diff(array_keys($collection->all()), $routeNamesBefore));
if ($newRouteName !== $aliasName = \sprintf('%s::%s', $class->name, $method->name)) {
$collection->addAlias($aliasName, $newRouteName);
}
}
}
// See https://symfony.com/doc/current/controller/service.html#invokable-controllers.
if ($collection->count() === 0 && $class->hasMethod('__invoke')) {
$globals = $this->resetGlobals();
foreach ($this->getAttributes($class) as $attribute) {
$attribute->defaults = [
'_controller' => $class->getName(),
] + $attribute->defaults;
$this->addRoute($collection, $attribute, $globals, $class, $class->getMethod('__invoke'));
$fqcnAlias = TRUE;
}
}
if ($fqcnAlias && $collection->count() === 1) {
$invokeRouteName = key($collection->all());
if ($invokeRouteName !== $class->name) {
$collection->addAlias($class->name, $invokeRouteName);
}
$aliasName = \sprintf('%s::__invoke', $class->name);
if ($aliasName !== $invokeRouteName) {
$collection->addAlias($aliasName, $invokeRouteName);
}
}
return $collection;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.