In symfony2 framework, how can I call a service outside the controller?


For example, I have defined a public service A named (domain)
In the controller we can directly
But what if I am not in the controller?

In the Controller, $this->get(‘ServiceName’) actually calls the Container Container, so whether it is in controller insIDe or not, the Container container is required to call the service, and the container container id is called service in Symfonycontainer,不管在哪里,你只需要注入 ServiceContainer can get the service:

Configure in services.yml inside:
 class: AppBundle\Form\YourServiceNamespace
 # inject a * * service ** (service)
 - @service_container
 # inject a * * parameter ** (parameter)
 -percentage parameterkey percentage
 # Inject a * * string ** (string)
 - "string"