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

  question

For example, I have defined a public service A named service.one (domain)
In the controller we can directly
$this->get(‘service.one’)->×××××
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:

service.id:
 class: AppBundle\Form\YourServiceNamespace
 arguments:
 # inject a * * service ** (service)
 - @service_container
 # inject a * * parameter ** (parameter)
 -percentage parameterkey percentage
 # Inject a * * string ** (string)
 - "string"