I'm refactoring one of my controller to make it a service and I want to know if there is a performance impact to not inject whole service container into my controller.
Is this is more efficient :
innova.path.controller:
class: %innova.controller.path.class%
arguments:
entityManager: @doctrine.orm.entity_manager
session: @session
securityContext: @security.context
router: @router
translator: @translator
pathManager: @innova.manager.path_manager
calls:
- [setRequest, ["@?request="]]
scope: request
than this, for example ?
innova.path.controller:
class: %innova.controller.path.class%
arguments: [@service_container]
Official documentation explicitly tell to not inject whole DIC into a Controller (thanks @NHG for link).
Section How to work with scopes :
Injecting the whole container into a service is generally not a good idea (only inject what you need).
But in section Service container :
When you ask for the my_mailer service from the container, the container constructs the object and returns it. This is another major advantage of using the service container. Namely, a service is never constructed until it's needed. If you define a service and never use it on a request, the service is never created. This saves memory and increases the speed of your application. This also means that there's very little or no performance hit for defining lots of services. Services that are never used are never constructed.
So injecting the whole DIC to controller will not have performances impact because only the services used in controller are instanciated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With