Contains \Drupal\Core\Controller\ControllerBase.

Namespace

Drupal\Core\Controller

File

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

Classes

Namesort descending Description
ControllerBase Utility base class for thin controllers.