interface ContainerInjectionInterface

You are here

Defines a common interface for route controllers.

This interface gives controller classes a factory method for instantiation rather than relying on a services.yml entry. However, it may result in a lot of boilerplate code in the class. As an alternative, controllers that contain only limited glue code ("thin" controllers) should instead extend ControllerBase as that allows direct access to the container. That renders the controller very difficult to unit test so should only be used for controllers that are trivial in complexity.



Implemented by


Expanded class hierarchy of ContainerInjectionInterface


Contains filters are case sensitive
Namesort descending Modifiers Type Description
ContainerInjectionInterface::create public static function Instantiates a new instance of this controller.


core/lib/Drupal/Core/DependencyInjection/ContainerInjectionInterface.php, line 23
Contains \Drupal\Core\DependencyInjection\ContainerInjectionInterface.

View source
interface ContainerInjectionInterface {

   * Instantiates a new instance of this controller.
   * This is a factory method that returns a new instance of this object. The
   * factory should pass any needed dependencies into the constructor of this
   * object, but not the container itself. Every call to this method must return
   * a new instance of this object; that is, it may not implement a singleton.
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container this object should use.
  public static function create(ContainerInterface $container);
20 files declare their use of ContainerInjectionInterface
BatchController.php in core/modules/system/lib/Drupal/system/Controller/BatchController.php
Contains \Drupal\system\Controller\BatchController.
BookController.php in core/modules/book/lib/Drupal/book/Controller/BookController.php
Contains \Drupal\book\Controller\BookController.
CategoryAutocompleteController.php in core/modules/block/lib/Drupal/block/Controller/CategoryAutocompleteController.php
Contains \Drupal\block\Controller\CategoryAutocompleteController.
ConfigController.php in core/modules/config/lib/Drupal/config/Controller/ConfigController.php
Contains \Drupal\config\Controller\ConfigController
ControllerBase.php in core/lib/Drupal/Core/Controller/ControllerBase.php
Contains \Drupal\Core\Controller\ControllerBase.

... See full list