Search code examples
drupaldrupal-8

Drupal 8 Controllers that extends ControllerBase and dependencies injection


I've seen tons of articles saying that I must use dependency injection in my Druapl8 controllers that extends ControllerBase (implementing "create" function etc...) However, ControllerBase allready has protected properties for getting common services such as entityTypeManager, etc. directly from the container.

So, can I use $this->entityTypeManager() directly inside a such a controller or do I have to do the whole stuff ("create" funciton etc...) ?

Thanks


Solution

  • Yes you can use some handy function currentUser(), config() and state() amongst others directly inside controller. Downside is it make unit testing harder.