abstract class ControllerBase

You are here

Utility base class for thin controllers.

Controllers that use this base class have access to a number of utility methods and to the Container, which can greatly reduce boilerplate dependency handling code. However, it also makes the class considerably more difficult to unit test. Therefore this base class should only be used by controller classes that contain only trivial glue code. Controllers that contain sufficiently complex logic that it's worth testing should not use this base class but use ContainerInjectionInterface instead, or even better be refactored to be trivial glue code.

The services exposed here are those that it is reasonable for a well-behaved controller to leverage. A controller that needs other other services may need to be refactored into a thin controller and a dependent unit-testable service.

Namespace

Drupal\Core\Controller

See also

\Drupal\Core\DependencyInjection\ContainerInjectionInterface

Hierarchy

Expanded class hierarchy of ControllerBase

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service.
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$formBuilder protected property The form builder.
ControllerBase::$keyValue protected property The key-value storage.
ControllerBase::$languageManager protected property The language manager.
ControllerBase::$moduleHandler protected property The module handler.
ControllerBase::$stateService protected property The state service.
ControllerBase::$translationManager protected property The translation manager.
ControllerBase::$urlGenerator protected property The url generator.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this controller. Overrides ContainerInjectionInterface::create
ControllerBase::currentUser protected function Returns the current user.
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager protected function Retrieves the entity manager service.
ControllerBase::formBuilder protected function Returns the form builder service.
ControllerBase::keyValue protected function Returns a key/value storage collection.
ControllerBase::l public function Renders a link to a route given a route name and its parameters.
ControllerBase::languageManager protected function Returns the language manager service.
ControllerBase::moduleHandler protected function Returns the module handler.
ControllerBase::redirect public function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
ControllerBase::t protected function Translates a string to the current language or to a given language.
ControllerBase::translationManager protected function Returns the translation manager.
ControllerBase::url public function Generates a URL or path for a specific route based on the given parameters.
ControllerBase::urlGenerator protected function Returns the URL generator service.

File

core/lib/Drupal/Core/Controller/ControllerBase.php, line 33
Contains \Drupal\Core\Controller\ControllerBase.

View source
abstract class ControllerBase implements ContainerInjectionInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The entity form builder.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface
   */
  protected $entityFormBuilder;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected $languageManager;

  /**
   * The translation manager.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationManager;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $configFactory;

  /**
   * The key-value storage.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $keyValue;

  /**
   * The url generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * The current user service.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The state service.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $stateService;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static();
  }

  /**
   * Retrieves the entity manager service.
   *
   * @return \Drupal\Core\Entity\EntityManagerInterface
   *   The entity manager service.
   */
  protected function entityManager() {
    if (!$this->entityManager) {
      $this->entityManager = $this->container()->get('entity.manager');
    }
    return $this->entityManager;
  }

  /**
   * Retrieves the entity form builder.
   *
   * @return \Drupal\Core\Entity\EntityFormBuilderInterface
   *   The entity form builder.
   */
  protected function entityFormBuilder() {
    if (!$this->entityFormBuilder) {
      $this->entityFormBuilder = $this->container()->get('entity.form_builder');
    }
    return $this->entityFormBuilder;
  }

  /**
   * Returns the requested cache bin.
   *
   * @param string $bin
   *   (optional) The cache bin for which the cache object should be returned,
   *   defaults to 'default'.
   *
   * @return \Drupal\Core\Cache\CacheBackendInterface
   *   The cache object associated with the specified bin.
   */
  protected function cache($bin = 'default') {
    return $this->container()->get('cache.' . $bin);
  }

  /**
   * Retrieves a configuration object.
   *
   * This is the main entry point to the configuration API. Calling
   * @code $this->config('book.admin') @endcode will return a configuration
   * object in which the book module can store its administrative settings.
   *
   * @param string $name
   *   The name of the configuration object to retrieve. The name corresponds to
   *   a configuration file. For @code \Drupal::config('book.admin') @endcode,
   *   the config object returned will contain the contents of book.admin
   *   configuration file.
   *
   * @return \Drupal\Core\Config\Config
   *   A configuration object.
   */
  protected function config($name) {
    if (!$this->configFactory) {
      $this->configFactory = $this->container()->get('config.factory');
    }
    return $this->configFactory->get($name);
  }

  /**
   * Returns a key/value storage collection.
   *
   * @param string $collection
   *   Name of the key/value collection to return.
   *
   * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected function keyValue($collection) {
    if (!$this->keyValue) {
      $this->keyValue = $this->container()->get('keyvalue')->get($collection);
    }
    return $this->keyValue;
  }

  /**
   * Returns the state storage service.
   *
   * Use this to store machine-generated data, local to a specific environment
   * that does not need deploying and does not need human editing; for example,
   * the last time cron was run. Data which needs to be edited by humans and
   * needs to be the same across development, production, etc. environments
   * (for example, the system maintenance message) should use config() instead.
   *
   * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected function state() {
    if (!$this->stateService) {
      $this->stateService = $this->container()->get('state');
    }
    return $this->stateService;
  }

  /**
   * Returns the module handler.
   *
   * @return \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected function moduleHandler() {
    if (!$this->moduleHandler) {
      $this->moduleHandler = $this->container()->get('module_handler');
    }
    return $this->moduleHandler;
  }

  /**
   * Returns the form builder service.
   *
   * @return \Drupal\Core\Form\FormBuilderInterface
   */
  protected function formBuilder() {
    if (!$this->formBuilder) {
      $this->formBuilder = $this->container()->get('form_builder');
    }
    return $this->formBuilder;
  }

  /**
   * Returns the URL generator service.
   *
   * @return \Drupal\Core\Routing\UrlGeneratorInterface
   *   The URL generator service.
   */
  protected function urlGenerator() {
    if (!$this->urlGenerator) {
      $this->urlGenerator = $this->container()->get('url_generator');
    }
    return $this->urlGenerator;
  }

  /**
   * Renders a link to a route given a route name and its parameters.
   *
   * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
   *   on the arguments, usage, and possible exceptions.
   *
   * @return string
   *   An HTML string containing a link to the given route and parameters.
   */
  public function l($text, $route_name, array $parameters = array(), array $options = array()) {
    return $this->container()->get('link_generator')->generate($text, $route_name, $parameters, $options);
  }

  /**
   * Returns the current user.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The current user.
   */
  protected function currentUser() {
    if (!$this->currentUser) {
      $this->currentUser = $this->container()->get('current_user');
    }
    return $this->currentUser;
  }

  /**
   * Translates a string to the current language or to a given language.
   *
   * See the t() documentation for details.
   */
  protected function t($string, array $args = array(), array $options = array()) {
    return $this->translationManager()->translate($string, $args, $options);
  }

  /**
   * Returns the translation manager.
   *
   * @return \Drupal\Core\StringTranslation\TranslationInterface
   *   The translation manager.
   */
  protected function translationManager() {
    if (!$this->translationManager) {
      $this->translationManager = $this->container()->get('string_translation');
    }
    return $this->translationManager;
  }

  /**
   * Returns the language manager service.
   *
   * @return \Drupal\Core\Language\LanguageManager
   *   The language manager.
   */
  protected function languageManager() {
    if (!$this->languageManager) {
      $this->languageManager = $this->container()->get('language_manager');
    }
    return $this->languageManager;
  }

  /**
   * Returns the service container.
   *
   * This method is marked private to prevent sub-classes from retrieving
   * services from the container through it. Instead,
   * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
   * for injecting services.
   *
   * @return \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container.
   */
  private function container() {
    return \Drupal::getContainer();
  }

  /**
   * Returns a redirect response object for the specified route.
   *
   * @param string $route_name
   *   The name of the route to which to redirect.
   * @param array $route_parameters
   *   Parameters for the route.
   * @param int $status
   *   The HTTP redirect status code for the redirect. The default is 302 Found.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect response object that may be returned by the controller.
   */
  public function redirect($route_name, array $route_parameters = array(), $status = 302) {
    $url = $this->urlGenerator()->generate($route_name, $route_parameters, TRUE);
    return new RedirectResponse($url, $status);
  }

  /**
   * Generates a URL or path for a specific route based on the given parameters.
   *
   * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
   *   details on the arguments, usage, and possible exceptions.
   *
   * @return string
   *   The generated URL for the given route.
   */
  public function url($route_name, $route_parameters = array(), $options = array()) {
    return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
  }

}
47 files declare their use of ControllerBase
AdminController.php in core/modules/comment/lib/Drupal/comment/Controller/AdminController.php
Contains \Drupal\comment\Controller\AdminController.
AdminController.php in core/modules/system/lib/Drupal/system/Controller/AdminController.php
Contains \Drupal\system\Controller\AdminController.
AggregatorController.php in core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php
Contains \Drupal\aggregator\Controller\AggregatorController.
AggregatorTestRssController.php in core/modules/aggregator/tests/modules/aggregator_test/lib/Drupal/aggregator_test/Controller/AggregatorTestRssController.php
Contains \Drupal\aggregator_test\Controller\AggregatorTestRssController.
BlockAddController.php in core/modules/block/lib/Drupal/block/Controller/BlockAddController.php
Contains \Drupal\block\Controller\BlockAddController.

... See full list