For example, I defined a public service a named Service.one.
In the controller, we can directly
$this->get (' Service.one ')->xxxxx
But what am I supposed to do if I'm not in the controller?
Reply content:
For example, I defined a public service a named Service.one.
In the controller, we can directly
$this->get (' Service.one ')->xxxxx
But what am I supposed to do if I'm not in the controller?
You must use the method container to container pass in a service or to be called. In fact controller , the get method in the call is also container the get method. Can inherit from ContainerAware , using setContainer method incoming container .
If it's not the controller, where are you? If it is another service, you can pass a service as a parameter when defining the service.
In the controller $this->get (' serviceName ') actually calls the Container container, so the call Service needs to be Container container, in Symfony, whether it is not in the controllers, Con Tainer container ID is called Servicecontainer, and wherever you are, you only need to injectservice container to get services:
Configuration in Services.yml:
service.id: class: AppBundle\Form\YourServiceNamespace arguments: # 注入一个 **服务** (service) - @service_container # 注入一个 **参数** (parameter) - %parameterkey% # 注入一个 **字符串** (string) - "string"