class ControllerResolver

Same name in other branches
  1. 9 core/lib/Drupal/Core/Controller/ControllerResolver.php \Drupal\Core\Controller\ControllerResolver
  2. 8.9.x core/lib/Drupal/Core/Controller/ControllerResolver.php \Drupal\Core\Controller\ControllerResolver
  3. 11.x core/lib/Drupal/Core/Controller/ControllerResolver.php \Drupal\Core\Controller\ControllerResolver

ControllerResolver to enhance controllers beyond Symfony's basic handling.

It adds two behaviors:

  • When creating a new object-based controller that implements ContainerAwareInterface, inject the container into it. While not always necessary, that allows a controller to vary the services it needs at runtime.
  • By default, a controller name follows the class::method notation. This class adds the possibility to use a service from the container as a controller by using a service:method notation (Symfony uses the same convention).

Hierarchy

  • class \Drupal\Core\Controller\ControllerResolver implements \Drupal\Core\Controller\ControllerResolverInterface

Expanded class hierarchy of ControllerResolver

1 file declares its use of ControllerResolver
ControllerResolverTest.php in core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php
1 string reference to 'ControllerResolver'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ControllerResolver
controller_resolver in core/core.services.yml
Drupal\Core\Controller\ControllerResolver

File

core/lib/Drupal/Core/Controller/ControllerResolver.php, line 25

Namespace

Drupal\Core\Controller
View source
class ControllerResolver implements ControllerResolverInterface {
    
    /**
     * Constructs a new ControllerResolver.
     *
     * @param \Drupal\Core\Utility\CallableResolver|\Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface $callableResolver
     *   The callable resolver.
     * @param \Drupal\Core\DependencyInjection\ClassResolverInterface|null $class_resolver
     *   The class resolver.
     */
    public function __construct(CallableResolver|HttpMessageFactoryInterface $callableResolver, ?ClassResolverInterface $class_resolver = NULL) {
        if ($callableResolver instanceof HttpMessageFactoryInterface) {
            @trigger_error('Calling ' . __METHOD__ . '() with the $http_message_factory argument is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3353869', E_USER_DEPRECATED);
            $this->callableResolver = \Drupal::service("callable_resolver");
        }
        if ($class_resolver !== NULL) {
            @trigger_error('Calling ' . __METHOD__ . '() with the $class_resolver argument is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3353869', E_USER_DEPRECATED);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getControllerFromDefinition($controller, $path = '') {
        try {
            $callable = $this->callableResolver
                ->getCallableFromDefinition($controller);
        } catch (\InvalidArgumentException $e) {
            throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path), 0, $e);
        }
        return $callable;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getController(Request $request) : callable|false {
        if (!($controller = $request->attributes
            ->get('_controller'))) {
            return FALSE;
        }
        return $this->getControllerFromDefinition($controller, $request->getPathInfo());
    }

}

Members

Title Sort descending Modifiers Object type Summary
ControllerResolver::getController public function
ControllerResolver::getControllerFromDefinition public function
ControllerResolver::__construct public function Constructs a new ControllerResolver.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.