Symfony 2: Dependency injection (DI) of Controllers Symfony 2: Dependency injection (DI) of Controllers symfony symfony

Symfony 2: Dependency injection (DI) of Controllers


Absolutely in fact it's recommended and if you look at most 3rd party bundles such as FOSUser you can see that that is exactly what they do.

The trick is to define your controllers as services and then use the service id instead of the class name.

http://symfony.com/doc/current/cookbook/controller/service.html

Keep in mind that you will have to inject all your needed services such as entity managers and you won't usually extend the symfony base class. Of course you could inject the complete container but that tends to be frowned on.