3 namespace Drupal\Core\Controller;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Logger\LoggerChannelTrait;
7 use Drupal\Core\Routing\LinkGeneratorTrait;
8 use Drupal\Core\Routing\RedirectDestinationTrait;
9 use Drupal\Core\Routing\UrlGeneratorTrait;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Drupal\Core\Messenger\MessengerTrait;
15 * Utility base class for thin controllers.
17 * Controllers that use this base class have access to a number of utility
18 * methods and to the Container, which can greatly reduce boilerplate dependency
19 * handling code. However, it also makes the class considerably more
20 * difficult to unit test. Therefore this base class should only be used by
21 * controller classes that contain only trivial glue code. Controllers that
22 * contain sufficiently complex logic that it's worth testing should not use
23 * this base class but use ContainerInjectionInterface instead, or even
24 * better be refactored to be trivial glue code.
26 * The services exposed here are those that it is reasonable for a well-behaved
27 * controller to leverage. A controller that needs other services may
28 * need to be refactored into a thin controller and a dependent unit-testable
31 * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
35 abstract class ControllerBase implements ContainerInjectionInterface {
37 use LinkGeneratorTrait;
38 use LoggerChannelTrait;
40 use RedirectDestinationTrait;
41 use StringTranslationTrait;
42 use UrlGeneratorTrait;
47 * @var \Drupal\Core\Entity\EntityManagerInterface
49 protected $entityManager;
52 * The entity type manager.
54 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
56 protected $entityTypeManager;
59 * The entity form builder.
61 * @var \Drupal\Core\Entity\EntityFormBuilderInterface
63 protected $entityFormBuilder;
66 * The language manager.
68 * @var \Drupal\Core\Language\LanguageManagerInterface
70 protected $languageManager;
73 * The configuration factory.
75 * @var \Drupal\Core\Config\ConfigFactoryInterface
77 protected $configFactory;
80 * The key-value storage.
82 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
87 * The current user service.
89 * @var \Drupal\Core\Session\AccountInterface
91 protected $currentUser;
96 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
98 protected $stateService;
101 * The module handler.
103 * @var \Drupal\Core\Extension\ModuleHandlerInterface
105 protected $moduleHandler;
110 * @var \Drupal\Core\Form\FormBuilderInterface
112 protected $formBuilder;
117 public static function create(ContainerInterface $container) {
122 * Retrieves the entity manager service.
124 * @return \Drupal\Core\Entity\EntityManagerInterface
125 * The entity manager service.
127 * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
128 * Most of the time static::entityTypeManager() is supposed to be used
131 protected function entityManager() {
132 if (!$this->entityManager) {
133 $this->entityManager = $this->container()->get('entity.manager');
135 return $this->entityManager;
139 * Retrieves the entity type manager.
141 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
142 * The entity type manager.
144 protected function entityTypeManager() {
145 if (!isset($this->entityTypeManager)) {
146 $this->entityTypeManager = $this->container()->get('entity_type.manager');
148 return $this->entityTypeManager;
152 * Retrieves the entity form builder.
154 * @return \Drupal\Core\Entity\EntityFormBuilderInterface
155 * The entity form builder.
157 protected function entityFormBuilder() {
158 if (!$this->entityFormBuilder) {
159 $this->entityFormBuilder = $this->container()->get('entity.form_builder');
161 return $this->entityFormBuilder;
165 * Returns the requested cache bin.
168 * (optional) The cache bin for which the cache object should be returned,
169 * defaults to 'default'.
171 * @return \Drupal\Core\Cache\CacheBackendInterface
172 * The cache object associated with the specified bin.
174 protected function cache($bin = 'default') {
175 return $this->container()->get('cache.' . $bin);
179 * Retrieves a configuration object.
181 * This is the main entry point to the configuration API. Calling
182 * @code $this->config('book.admin') @endcode will return a configuration
183 * object in which the book module can store its administrative settings.
185 * @param string $name
186 * The name of the configuration object to retrieve. The name corresponds to
187 * a configuration file. For @code \Drupal::config('book.admin') @endcode,
188 * the config object returned will contain the contents of book.admin
189 * configuration file.
191 * @return \Drupal\Core\Config\Config
192 * A configuration object.
194 protected function config($name) {
195 if (!$this->configFactory) {
196 $this->configFactory = $this->container()->get('config.factory');
198 return $this->configFactory->get($name);
202 * Returns a key/value storage collection.
204 * @param string $collection
205 * Name of the key/value collection to return.
207 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
209 protected function keyValue($collection) {
210 if (!$this->keyValue) {
211 $this->keyValue = $this->container()->get('keyvalue')->get($collection);
213 return $this->keyValue;
217 * Returns the state storage service.
219 * Use this to store machine-generated data, local to a specific environment
220 * that does not need deploying and does not need human editing; for example,
221 * the last time cron was run. Data which needs to be edited by humans and
222 * needs to be the same across development, production, etc. environments
223 * (for example, the system maintenance message) should use config() instead.
225 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
227 protected function state() {
228 if (!$this->stateService) {
229 $this->stateService = $this->container()->get('state');
231 return $this->stateService;
235 * Returns the module handler.
237 * @return \Drupal\Core\Extension\ModuleHandlerInterface
239 protected function moduleHandler() {
240 if (!$this->moduleHandler) {
241 $this->moduleHandler = $this->container()->get('module_handler');
243 return $this->moduleHandler;
247 * Returns the form builder service.
249 * @return \Drupal\Core\Form\FormBuilderInterface
251 protected function formBuilder() {
252 if (!$this->formBuilder) {
253 $this->formBuilder = $this->container()->get('form_builder');
255 return $this->formBuilder;
259 * Returns the current user.
261 * @return \Drupal\Core\Session\AccountInterface
264 protected function currentUser() {
265 if (!$this->currentUser) {
266 $this->currentUser = $this->container()->get('current_user');
268 return $this->currentUser;
272 * Returns the language manager service.
274 * @return \Drupal\Core\Language\LanguageManagerInterface
275 * The language manager.
277 protected function languageManager() {
278 if (!$this->languageManager) {
279 $this->languageManager = $this->container()->get('language_manager');
281 return $this->languageManager;
285 * Returns the service container.
287 * This method is marked private to prevent sub-classes from retrieving
288 * services from the container through it. Instead,
289 * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
290 * for injecting services.
292 * @return \Symfony\Component\DependencyInjection\ContainerInterface
293 * The service container.
295 private function container() {
296 return \Drupal::getContainer();