2017-03-02 68 views
0

我想配置一个服务来自动将一个现有的DI容器注入到它。我ContentQuery类已经extends另一个类,所以它必须改为实行ContainerAwareInterface和使用call块配置实际注射:如何将Symfony DI容器本身注入到服务中?

<services> 
    <service id="my_module.model.content_query" class="MyModule\Model\ContentQuery"> 
    <call method="setContainer"> 
     <argument type="service" id="container" /> 
    </call> 
    </service> 
</services> 

的问题是,我不知道如何来识别XML容器本身。使用上面的配置,我得到这个错误:

the service "my_module.model.content_query" has a dependency on a non-existent service "container"

我宁愿只是这样做在PHP中,我认为我能想出很容易,但是要使用XML的决定是从我手中的这个时候。那么有没有办法将Container实例识别为服务?

回答

0

Welp。发布后大约一分钟,我就明白了。 :P

只需要指定id="service_container",而不是仅仅 “容器”:

<services> 
    <service id="my_module.model.content_query" class="MyModule\Model\ContentQuery"> 
    <call method="setContainer"> 
     <argument type="service" id="service_container" /> 
    </call> 
    </service> 
</services> 
+2

应用程序/控制台调试:容器是你的朋友 – Cerad