Reputation: 35
I have multiple functions in my controller, and lots of them call $usermgr = $this->get('usermanager');
(where usermanager is a service defined in config.yml).
This means code is repeated multiple times. Is there a way to define $usermgr only once in the controller? Normally I'd consider a __construct but I believe thats not possible with a controller?
Upvotes: 0
Views: 203
Reputation: 41934
There is nothing wrong with getting the same service in multiple methods in a controller.
You can change your controller into a service ( http://symfony.com/doc/current/cookbook/controller/service.html and http://richardmiller.co.uk/2011/04/15/symfony2-controller-as-service/ ) and then inject that service. While that follows the best practises, your current way is not wrong.
You cannot request that service in the constructor, using the Symfony base Controller
class, as the service container is set after initialization.
Upvotes: 1