gpt4 book ai didi

Symfony2 : How to access a service inside an extension's load() method?

转载 作者:行者123 更新时间:2023-12-02 16:13:23 25 4
gpt4 key购买 nike

是否可以在捆绑扩展中获取 Doctrine 服务?

我可以访问容器,但无法获取 Doctrine 服务。

...
class UltroExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container){
$dm = $container->get('doctrine_mongodb');
...
}
}

我收到此错误:服务定义“doctrine_mongodb”不存在。

最佳答案

容器尚未构建/编译 - 这就是为什么您只获得传递给 load() 方法的 ContainerBuiler 对象。

您无法从构建器获取服务对象,因为此时它不保存服务,而仅保存服务定义

使用 compiler pass 相反。更多信息可以在文档章节 Compiling the container 中找到。

也许您的问题可以使用 service factory 来解决,也是。

关于Symfony2 : How to access a service inside an extension's load() method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23721045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com