class ControllerResolver

Same name and namespace 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. 10 core/lib/Drupal/Core/Controller/ControllerResolver.php \Drupal\Core\Controller\ControllerResolver

ControllerResolver to enhance controllers beyond Symfony's basic handling.

It adds one behavior:

  • 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

Expanded class hierarchy of ControllerResolver

1 file declares its use of ControllerResolver
ControllerResolverTest.php in core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php

File

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

Namespace

Drupal\Core\Controller
View source
class ControllerResolver implements ControllerResolverInterface {
    
    /**
     * Constructs a new ControllerResolver.
     *
     * @param \Drupal\Core\Utility\CallableResolver $callableResolver
     *   The callable resolver.
     */
    public function __construct(CallableResolver $callableResolver) {
    }
    
    /**
     * {@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 Overriden Title
ControllerResolver::getController public function
ControllerResolver::getControllerFromDefinition public function Returns the Controller instance with a given controller route definition. Overrides ControllerResolverInterface::getControllerFromDefinition
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.