class ControllerResolver

Same name in this branch

ControllerResolver to enhance controllers beyond Symfony's basic handling.

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.

Hierarchy

Expanded class hierarchy of ControllerResolver

1 file declares its use of ControllerResolver
ControllerResolverTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Routing/ControllerResolverTest.php
Definition of Drupal\system\Tests\Routing\ControllerResolverTest.

File

drupal/core/lib/Drupal/Core/ControllerResolver.php, line 22
Definition of Drupal\Core\ControllerResolver.

Namespace

Drupal\Core
View source
class ControllerResolver extends BaseControllerResolver {

  /**
   * The injection container that should be injected into all controllers.
   *
   * @var Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * Constructs a new ControllerResolver.
   *
   * @param Symfony\Component\DependencyInjection\ContainerInterface $container
   *   A ContainerInterface instance.
   * @param Symfony\Component\HttpKernel\Log\LoggerInterface $logger
   *   (optional) A LoggerInterface instance.
   */
  public function __construct(ContainerInterface $container, LoggerInterface $logger = NULL) {
    $this->container = $container;
    parent::__construct($logger);
  }

  /**
   * Returns a callable for the given controller.
   *
   * @param string $controller
   *   A Controller string.
   *
   * @return mixed
   *   A PHP callable.
   */
  protected function createController($controller) {
    $controller = parent::createController($controller);

    // $controller will be an array of object and method name, per PHP's
    // definition of a callable. Index 0 therefore is the object we want to
    // enhance.
    if ($controller[0] instanceof ContainerAwareInterface) {
      $controller[0]
        ->setContainer($this->container);
    }
    return $controller;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerResolver::$container protected property The injection container that should be injected into all controllers.
ControllerResolver::$logger private property
ControllerResolver::createController protected function Returns a callable for the given controller. Overrides ControllerResolver::createController
ControllerResolver::doGetArguments protected function
ControllerResolver::getArguments public function Returns the arguments to pass to the controller. Overrides ControllerResolverInterface::getArguments
ControllerResolver::getController public function Returns the Controller instance associated with a Request. Overrides ControllerResolverInterface::getController
ControllerResolver::__construct public function Constructs a new ControllerResolver. Overrides ControllerResolver::__construct