1. 8.2.x core/lib/Drupal/Core/Controller/ControllerBase.php
  2. 8.0.x core/lib/Drupal/Core/Controller/ControllerBase.php
  3. 8.1.x core/lib/Drupal/Core/Controller/ControllerBase.php

Namespace

Drupal\Core\Controller

File

core/lib/Drupal/Core/Controller/ControllerBase.php
View source
  1. <?php
  2. namespace Drupal\Core\Controller;
  3. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  4. use Drupal\Core\Logger\LoggerChannelTrait;
  5. use Drupal\Core\Routing\LinkGeneratorTrait;
  6. use Drupal\Core\Routing\RedirectDestinationTrait;
  7. use Drupal\Core\Routing\UrlGeneratorTrait;
  8. use Drupal\Core\StringTranslation\StringTranslationTrait;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Utility base class for thin controllers.
  12. *
  13. * Controllers that use this base class have access to a number of utility
  14. * methods and to the Container, which can greatly reduce boilerplate dependency
  15. * handling code. However, it also makes the class considerably more
  16. * difficult to unit test. Therefore this base class should only be used by
  17. * controller classes that contain only trivial glue code. Controllers that
  18. * contain sufficiently complex logic that it's worth testing should not use
  19. * this base class but use ContainerInjectionInterface instead, or even better be
  20. * refactored to be trivial glue code.
  21. *
  22. * The services exposed here are those that it is reasonable for a well-behaved
  23. * controller to leverage. A controller that needs other services may
  24. * need to be refactored into a thin controller and a dependent unit-testable
  25. * service.
  26. *
  27. * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
  28. *
  29. * @ingroup routing
  30. */
  31. abstract class ControllerBase implements ContainerInjectionInterface {
  32. use LinkGeneratorTrait;
  33. use LoggerChannelTrait;
  34. use RedirectDestinationTrait;
  35. use StringTranslationTrait;
  36. use UrlGeneratorTrait;
  37. /**
  38. * The entity manager.
  39. *
  40. * @var \Drupal\Core\Entity\EntityManagerInterface
  41. */
  42. protected $entityManager;
  43. /**
  44. * The entity type manager.
  45. *
  46. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  47. */
  48. protected $entityTypeManager;
  49. /**
  50. * The entity form builder.
  51. *
  52. * @var \Drupal\Core\Entity\EntityFormBuilderInterface
  53. */
  54. protected $entityFormBuilder;
  55. /**
  56. * The language manager.
  57. *
  58. * @var \Drupal\Core\Language\LanguageManagerInterface
  59. */
  60. protected $languageManager;
  61. /**
  62. * The configuration factory.
  63. *
  64. * @var \Drupal\Core\Config\Config
  65. */
  66. protected $configFactory;
  67. /**
  68. * The key-value storage.
  69. *
  70. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  71. */
  72. protected $keyValue;
  73. /**
  74. * The current user service.
  75. *
  76. * @var \Drupal\Core\Session\AccountInterface
  77. */
  78. protected $currentUser;
  79. /**
  80. * The state service.
  81. *
  82. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  83. */
  84. protected $stateService;
  85. /**
  86. * The module handler.
  87. *
  88. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  89. */
  90. protected $moduleHandler;
  91. /**
  92. * The form builder.
  93. *
  94. * @var \Drupal\Core\Form\FormBuilderInterface
  95. */
  96. protected $formBuilder;
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public static function create(ContainerInterface $container) {
  101. return new static();
  102. }
  103. /**
  104. * Retrieves the entity manager service.
  105. *
  106. * @return \Drupal\Core\Entity\EntityManagerInterface
  107. * The entity manager service.
  108. *
  109. * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
  110. * Most of the time static::entityTypeManager() is supposed to be used
  111. * instead.
  112. */
  113. protected function entityManager() {
  114. if (!$this->entityManager) {
  115. $this->entityManager = $this->container()->get('entity.manager');
  116. }
  117. return $this->entityManager;
  118. }
  119. /**
  120. * Retrieves the entity type manager.
  121. *
  122. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  123. * The entity type manager.
  124. */
  125. protected function entityTypeManager() {
  126. if (!isset($this->entityTypeManager)) {
  127. $this->entityTypeManager = $this->container()->get('entity_type.manager');
  128. }
  129. return $this->entityTypeManager;
  130. }
  131. /**
  132. * Retrieves the entity form builder.
  133. *
  134. * @return \Drupal\Core\Entity\EntityFormBuilderInterface
  135. * The entity form builder.
  136. */
  137. protected function entityFormBuilder() {
  138. if (!$this->entityFormBuilder) {
  139. $this->entityFormBuilder = $this->container()->get('entity.form_builder');
  140. }
  141. return $this->entityFormBuilder;
  142. }
  143. /**
  144. * Returns the requested cache bin.
  145. *
  146. * @param string $bin
  147. * (optional) The cache bin for which the cache object should be returned,
  148. * defaults to 'default'.
  149. *
  150. * @return \Drupal\Core\Cache\CacheBackendInterface
  151. * The cache object associated with the specified bin.
  152. */
  153. protected function cache($bin = 'default') {
  154. return $this->container()->get('cache.' . $bin);
  155. }
  156. /**
  157. * Retrieves a configuration object.
  158. *
  159. * This is the main entry point to the configuration API. Calling
  160. * @code $this->config('book.admin') @endcode will return a configuration
  161. * object in which the book module can store its administrative settings.
  162. *
  163. * @param string $name
  164. * The name of the configuration object to retrieve. The name corresponds to
  165. * a configuration file. For @code \Drupal::config('book.admin') @endcode,
  166. * the config object returned will contain the contents of book.admin
  167. * configuration file.
  168. *
  169. * @return \Drupal\Core\Config\Config
  170. * A configuration object.
  171. */
  172. protected function config($name) {
  173. if (!$this->configFactory) {
  174. $this->configFactory = $this->container()->get('config.factory');
  175. }
  176. return $this->configFactory->get($name);
  177. }
  178. /**
  179. * Returns a key/value storage collection.
  180. *
  181. * @param string $collection
  182. * Name of the key/value collection to return.
  183. *
  184. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  185. */
  186. protected function keyValue($collection) {
  187. if (!$this->keyValue) {
  188. $this->keyValue = $this->container()->get('keyvalue')->get($collection);
  189. }
  190. return $this->keyValue;
  191. }
  192. /**
  193. * Returns the state storage service.
  194. *
  195. * Use this to store machine-generated data, local to a specific environment
  196. * that does not need deploying and does not need human editing; for example,
  197. * the last time cron was run. Data which needs to be edited by humans and
  198. * needs to be the same across development, production, etc. environments
  199. * (for example, the system maintenance message) should use config() instead.
  200. *
  201. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  202. */
  203. protected function state() {
  204. if (!$this->stateService) {
  205. $this->stateService = $this->container()->get('state');
  206. }
  207. return $this->stateService;
  208. }
  209. /**
  210. * Returns the module handler.
  211. *
  212. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  213. */
  214. protected function moduleHandler() {
  215. if (!$this->moduleHandler) {
  216. $this->moduleHandler = $this->container()->get('module_handler');
  217. }
  218. return $this->moduleHandler;
  219. }
  220. /**
  221. * Returns the form builder service.
  222. *
  223. * @return \Drupal\Core\Form\FormBuilderInterface
  224. */
  225. protected function formBuilder() {
  226. if (!$this->formBuilder) {
  227. $this->formBuilder = $this->container()->get('form_builder');
  228. }
  229. return $this->formBuilder;
  230. }
  231. /**
  232. * Returns the current user.
  233. *
  234. * @return \Drupal\Core\Session\AccountInterface
  235. * The current user.
  236. */
  237. protected function currentUser() {
  238. if (!$this->currentUser) {
  239. $this->currentUser = $this->container()->get('current_user');
  240. }
  241. return $this->currentUser;
  242. }
  243. /**
  244. * Returns the language manager service.
  245. *
  246. * @return \Drupal\Core\Language\LanguageManagerInterface
  247. * The language manager.
  248. */
  249. protected function languageManager() {
  250. if (!$this->languageManager) {
  251. $this->languageManager = $this->container()->get('language_manager');
  252. }
  253. return $this->languageManager;
  254. }
  255. /**
  256. * Returns the service container.
  257. *
  258. * This method is marked private to prevent sub-classes from retrieving
  259. * services from the container through it. Instead,
  260. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
  261. * for injecting services.
  262. *
  263. * @return \Symfony\Component\DependencyInjection\ContainerInterface $container
  264. * The service container.
  265. */
  266. private function container() {
  267. return \Drupal::getContainer();
  268. }
  269. }

Classes

Namesort descending Description
ControllerBase Utility base class for thin controllers.