Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Controller/ControllerBase.php \Drupal\Core\Controller\ControllerBase
  2. 9 core/lib/Drupal/Core/Controller/ControllerBase.php \Drupal\Core\Controller\ControllerBase

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 services may need to be refactored into a thin controller and a dependent unit-testable service.

Hierarchy

Expanded class hierarchy of ControllerBase

See also

\Drupal\Core\DependencyInjection\ContainerInjectionInterface

Related topics

74 files declare their use of ControllerBase
BlockAddController.php in core/modules/block/src/Controller/BlockAddController.php
BlockContentController.php in core/modules/block_content/src/Controller/BlockContentController.php
BlockController.php in core/modules/block/src/Controller/BlockController.php
BlockLibraryController.php in core/modules/block/src/Controller/BlockLibraryController.php
BookController.php in core/modules/book/src/Controller/BookController.php

... See full list

File

core/lib/Drupal/Core/Controller/ControllerBase.php, line 35

Namespace

Drupal\Core\Controller
View source
abstract class ControllerBase implements ContainerInjectionInterface {
  use AutowireTrait;
  use LoggerChannelTrait;
  use MessengerTrait;
  use RedirectDestinationTrait;
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

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

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

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

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

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $stateService;

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

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

  /**
   * Retrieves the entity type manager.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The entity type manager.
   */
  protected function entityTypeManager() {
    if (!isset($this->entityTypeManager)) {
      $this->entityTypeManager = $this
        ->container()
        ->get('entity_type.manager');
    }
    return $this->entityTypeManager;
  }

  /**
   * 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('my_module.admin') @endcode will return a configuration
   * object in which the my_module 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('my_module.admin') @endcode,
   *   the config object returned will contain the contents of my_module.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\State\StateInterface
   */
  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 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;
  }

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

  /**
   * 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
   *   (optional) Parameters for the route.
   * @param array $options
   *   (optional) An associative array of additional options.
   * @param int $status
   *   (optional) 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.
   */
  protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
    $options['absolute'] = TRUE;
    return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)
      ->toString(), $status);
  }

  /**
   * 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
   *   The service container.
   */
  private function container() {
    return \Drupal::getContainer();
  }

}

Members

Name Modifiers Typesort descending Description Overrides
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::state protected function Returns the state storage service.
ControllerBase::moduleHandler protected function Returns the module handler.
ControllerBase::formBuilder protected function Returns the form builder service.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::container private function Returns the service container.
AutowireTrait::create public static function Instantiates a new instance of the implementing class using autowiring. 20
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::setMessenger public function Sets the messenger.
MessengerTrait::messenger public function Gets the messenger. 8
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 1
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$stateService protected property The state service.
ControllerBase::$moduleHandler protected property The module handler.
ControllerBase::$formBuilder protected property The form builder.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
MessengerTrait::$messenger protected property The messenger. 8
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 3