Decouple Controller from Symfony using Traits

Since Symfony 3.3 you can use AbstractController. It does pretty much the same thing - in even cleaner way - and it has native support in Symfony.

I recommend using it instead!

If you start using controllers as services, you still often need helpers methods of Controller from FrameworkBundle. So your code still depends on service locator and decoupling is not really happening.

Today I will show you how to remove the dependency on Controller and keep those fancy methods at the same time.

Typical use case of Mixing Principles

Constructor Injection

Service locator (container) from parent Controller class

Today it's not about which one is better and when to use it, but about consistency. I recommend either using first or second. Mixing them together is confusing and adds complexity without bringing value or improving readability.