ControllerBase.php

You are here

Contains \Drupal\Core\Controller\ControllerBase.

Namespace

Drupal\Core\Controller

Classes

Namesort descending Description
ControllerBase Utility base class for thin controllers.

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 Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  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 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. abstract class ControllerBase implements ContainerInjectionInterface {
  30. /**
  31. * The entity manager.
  32. *
  33. * @var \Drupal\Core\Entity\EntityManagerInterface
  34. */
  35. protected $entityManager;
  36. /**
  37. * The entity form builder.
  38. *
  39. * @var \Drupal\Core\Entity\EntityFormBuilderInterface
  40. */
  41. protected $entityFormBuilder;
  42. /**
  43. * The language manager.
  44. *
  45. * @var \Drupal\Core\Language\LanguageManager
  46. */
  47. protected $languageManager;
  48. /**
  49. * The translation manager.
  50. *
  51. * @var \Drupal\Core\StringTranslation\TranslationInterface
  52. */
  53. protected $translationManager;
  54. /**
  55. * The configuration factory.
  56. *
  57. * @var \Drupal\Core\Config\Config
  58. */
  59. protected $configFactory;
  60. /**
  61. * The key-value storage.
  62. *
  63. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  64. */
  65. protected $keyValue;
  66. /**
  67. * The url generator.
  68. *
  69. * @var \Drupal\Core\Routing\UrlGeneratorInterface
  70. */
  71. protected $urlGenerator;
  72. /**
  73. * The current user service.
  74. *
  75. * @var \Drupal\Core\Session\AccountInterface
  76. */
  77. protected $currentUser;
  78. /**
  79. * The state service.
  80. *
  81. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  82. */
  83. protected $stateService;
  84. /**
  85. * The module handler.
  86. *
  87. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  88. */
  89. protected $moduleHandler;
  90. /**
  91. * The form builder.
  92. *
  93. * @var \Drupal\Core\Form\FormBuilderInterface
  94. */
  95. protected $formBuilder;
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public static function create(ContainerInterface $container) {
  100. return new static();
  101. }
  102. /**
  103. * Retrieves the entity manager service.
  104. *
  105. * @return \Drupal\Core\Entity\EntityManagerInterface
  106. * The entity manager service.
  107. */
  108. protected function entityManager() {
  109. if (!$this->entityManager) {
  110. $this->entityManager = $this->container()->get('entity.manager');
  111. }
  112. return $this->entityManager;
  113. }
  114. /**
  115. * Retrieves the entity form builder.
  116. *
  117. * @return \Drupal\Core\Entity\EntityFormBuilderInterface
  118. * The entity form builder.
  119. */
  120. protected function entityFormBuilder() {
  121. if (!$this->entityFormBuilder) {
  122. $this->entityFormBuilder = $this->container()->get('entity.form_builder');
  123. }
  124. return $this->entityFormBuilder;
  125. }
  126. /**
  127. * Returns the requested cache bin.
  128. *
  129. * @param string $bin
  130. * (optional) The cache bin for which the cache object should be returned,
  131. * defaults to 'default'.
  132. *
  133. * @return \Drupal\Core\Cache\CacheBackendInterface
  134. * The cache object associated with the specified bin.
  135. */
  136. protected function cache($bin = 'default') {
  137. return $this->container()->get('cache.' . $bin);
  138. }
  139. /**
  140. * Retrieves a configuration object.
  141. *
  142. * This is the main entry point to the configuration API. Calling
  143. * @code $this->config('book.admin') @endcode will return a configuration
  144. * object in which the book module can store its administrative settings.
  145. *
  146. * @param string $name
  147. * The name of the configuration object to retrieve. The name corresponds to
  148. * a configuration file. For @code \Drupal::config('book.admin') @endcode,
  149. * the config object returned will contain the contents of book.admin
  150. * configuration file.
  151. *
  152. * @return \Drupal\Core\Config\Config
  153. * A configuration object.
  154. */
  155. protected function config($name) {
  156. if (!$this->configFactory) {
  157. $this->configFactory = $this->container()->get('config.factory');
  158. }
  159. return $this->configFactory->get($name);
  160. }
  161. /**
  162. * Returns a key/value storage collection.
  163. *
  164. * @param string $collection
  165. * Name of the key/value collection to return.
  166. *
  167. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  168. */
  169. protected function keyValue($collection) {
  170. if (!$this->keyValue) {
  171. $this->keyValue = $this->container()->get('keyvalue')->get($collection);
  172. }
  173. return $this->keyValue;
  174. }
  175. /**
  176. * Returns the state storage service.
  177. *
  178. * Use this to store machine-generated data, local to a specific environment
  179. * that does not need deploying and does not need human editing; for example,
  180. * the last time cron was run. Data which needs to be edited by humans and
  181. * needs to be the same across development, production, etc. environments
  182. * (for example, the system maintenance message) should use config() instead.
  183. *
  184. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  185. */
  186. protected function state() {
  187. if (!$this->stateService) {
  188. $this->stateService = $this->container()->get('state');
  189. }
  190. return $this->stateService;
  191. }
  192. /**
  193. * Returns the module handler.
  194. *
  195. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  196. */
  197. protected function moduleHandler() {
  198. if (!$this->moduleHandler) {
  199. $this->moduleHandler = $this->container()->get('module_handler');
  200. }
  201. return $this->moduleHandler;
  202. }
  203. /**
  204. * Returns the form builder service.
  205. *
  206. * @return \Drupal\Core\Form\FormBuilderInterface
  207. */
  208. protected function formBuilder() {
  209. if (!$this->formBuilder) {
  210. $this->formBuilder = $this->container()->get('form_builder');
  211. }
  212. return $this->formBuilder;
  213. }
  214. /**
  215. * Returns the URL generator service.
  216. *
  217. * @return \Drupal\Core\Routing\UrlGeneratorInterface
  218. * The URL generator service.
  219. */
  220. protected function urlGenerator() {
  221. if (!$this->urlGenerator) {
  222. $this->urlGenerator = $this->container()->get('url_generator');
  223. }
  224. return $this->urlGenerator;
  225. }
  226. /**
  227. * Renders a link to a route given a route name and its parameters.
  228. *
  229. * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
  230. * on the arguments, usage, and possible exceptions.
  231. *
  232. * @return string
  233. * An HTML string containing a link to the given route and parameters.
  234. */
  235. public function l($text, $route_name, array $parameters = array(), array $options = array()) {
  236. return $this->container()->get('link_generator')->generate($text, $route_name, $parameters, $options);
  237. }
  238. /**
  239. * Returns the current user.
  240. *
  241. * @return \Drupal\Core\Session\AccountInterface
  242. * The current user.
  243. */
  244. protected function currentUser() {
  245. if (!$this->currentUser) {
  246. $this->currentUser = $this->container()->get('current_user');
  247. }
  248. return $this->currentUser;
  249. }
  250. /**
  251. * Translates a string to the current language or to a given language.
  252. *
  253. * See the t() documentation for details.
  254. */
  255. protected function t($string, array $args = array(), array $options = array()) {
  256. return $this->translationManager()->translate($string, $args, $options);
  257. }
  258. /**
  259. * Returns the translation manager.
  260. *
  261. * @return \Drupal\Core\StringTranslation\TranslationInterface
  262. * The translation manager.
  263. */
  264. protected function translationManager() {
  265. if (!$this->translationManager) {
  266. $this->translationManager = $this->container()->get('string_translation');
  267. }
  268. return $this->translationManager;
  269. }
  270. /**
  271. * Returns the language manager service.
  272. *
  273. * @return \Drupal\Core\Language\LanguageManager
  274. * The language manager.
  275. */
  276. protected function languageManager() {
  277. if (!$this->languageManager) {
  278. $this->languageManager = $this->container()->get('language_manager');
  279. }
  280. return $this->languageManager;
  281. }
  282. /**
  283. * Returns the service container.
  284. *
  285. * This method is marked private to prevent sub-classes from retrieving
  286. * services from the container through it. Instead,
  287. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
  288. * for injecting services.
  289. *
  290. * @return \Symfony\Component\DependencyInjection\ContainerInterface $container
  291. * The service container.
  292. */
  293. private function container() {
  294. return \Drupal::getContainer();
  295. }
  296. /**
  297. * Returns a redirect response object for the specified route.
  298. *
  299. * @param string $route_name
  300. * The name of the route to which to redirect.
  301. * @param array $route_parameters
  302. * Parameters for the route.
  303. * @param int $status
  304. * The HTTP redirect status code for the redirect. The default is 302 Found.
  305. *
  306. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  307. * A redirect response object that may be returned by the controller.
  308. */
  309. public function redirect($route_name, array $route_parameters = array(), $status = 302) {
  310. $url = $this->urlGenerator()->generate($route_name, $route_parameters, TRUE);
  311. return new RedirectResponse($url, $status);
  312. }
  313. /**
  314. * Generates a URL or path for a specific route based on the given parameters.
  315. *
  316. * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for
  317. * details on the arguments, usage, and possible exceptions.
  318. *
  319. * @return string
  320. * The generated URL for the given route.
  321. */
  322. public function url($route_name, $route_parameters = array(), $options = array()) {
  323. return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
  324. }
  325. }