在Symfony中用不同别名注入相同的服务
P粉662802882
P粉662802882 2023-08-07 21:55:08
[PHP讨论组]
<p>我不确定问的是否准确,那我解释一下<br /><br />我用foselasticabundance与此配置:</p><p><br /></p> <pre class="brush:php;toolbar:false;"># Read the documentation: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/setup.md fos_elastica: clients: default: { url: '%env(ELASTICSEARCH_URL)%' } indexes: articles: index_name: 'articles' persistence: # the driver can be orm, mongodb or phpcr driver: orm model: AppEntitySiteFArticleFArticleProp provider: ~ finder: ~</pre> <p>要<span style="white-space:normal;">这样</span>查询我的文章索引:</p> <pre class="brush:php;toolbar:false;">&lt;?php namespace AppService; use FOSElasticaBundleFinderTransformedFinder; class FArticleService { public function __construct( private readonly TransformedFinder $finder, ) { } public function query(array $query, int $page, int $size) { $results = $this-&gt;finder-&gt;createRawPaginatorAdapter($query)-&gt;getResults(($page * $size) - $size, $size); } }</pre> <p>但是有个错:</p> <pre class="brush:php;toolbar:false;">In DefinitionErrorExceptionPass.php line 51: Cannot autowire service "AppServiceFArticleService": argument "$finder" of method "__construct()" references class "FOSElasticaBundleFinderTransformedFinder" but no such service exists. You should maybe alias this class to the existing "fos_elastica.finder.articles" service.</pre> <p>我改了一下</p> <pre class="brush:php;toolbar:false;"># api/config/services.yaml services: #... FOSElasticaBundleFinderTransformedFinder: alias: fos_elastica.finder.articles public: true</pre> <p>这样还挺好<br /><br />但现在我要增加第二个指标,0百分</p><p><code></code></p> <pre class="lang-yaml prettyprint-override"><code># Read the documentation: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/setup.md fos_elastica: clients: default: { url: '%env(ELASTICSEARCH_URL)%' } indexes: articles: index_name: 'articles' persistence: # the driver can be orm, mongodb or phpcr driver: orm model: AppEntitySiteFArticleFArticleProp provider: ~ finder: ~ fdocentetes: index_name: 'fdocentetes' persistence: # the driver can be orm, mongodb or phpcr driver: orm model: AppEntitySageFDocentete provider: ~ finder: ~ </code></pre> <p>但现在我不知道如何查询fdocenttetes索引作为transformmedfinder有别名fos_elastica.finder。文章,所以如果我创建另一个服务fdocteteservice:</p> <pre class="lang-php prettyprint-override"><code>&lt;?php namespace AppService; use FOSElasticaBundleFinderTransformedFinder; class FDocenteteService { public function __construct( private readonly TransformedFinder $finder, ) { } public function query(array $query, int $page, int $size) { // this will query on the index articles while I want to query on index fdocentetes $results = $this-&gt;finder-&gt;createRawPaginatorAdapter($query)-&gt;getResults(($page * $size) - $size, $size); } } </code></pre> <p>那现在咋办</p> <pre class="brush:php;toolbar:false;">$ php bin/console debug:container | grep fos_elastica.finder FOSElasticaBundleFinderTransformedFinder alias for "fos_elastica.finder.articles" fos_elastica.finder FOSElasticaBundleFinderTransformedFinder fos_elastica.finder.articles FOSElasticaBundleFinderTransformedFinder fos_elastica.finder.fdocentetes FOSElasticaBundleFinderTransformedFinder</pre> <pre class="brush:php;toolbar:false;">$ php bin/console debug:autowiring | grep fos_elastica.finder FOSElasticaBundleFinderTransformedFinder (fos_elastica.finder.articles)</pre> <p><br /></p>
P粉662802882
P粉662802882

全部回复(1)
P粉777458787

这是我从现有项目的解决方案。

app.service.configuration_service_foo:
    public: true
    class: App\Service\ConfigurationService
    arguments:
      - '@app.config.foo_config'

app.service.configuration_service_bar:
    public: true
    class: App\Service\ConfigurationService
    arguments:
      - '@app.config.bar_config'
private ConfigurationService $configServiceFoo;
private ConfigurationService $configServiceBar;

public function __construct(
    ContainerInterface $container,
) {
    $this->configServiceFoo = clone $container->get('app.service.configuration_service_foo');
    $this->configServiceBar = clone $container->get('app.service.configuration_service_bar');
}

也应该还有别的方法。

不可能通过自动调用两次注入同一个对象。所以,注入了ContainerInterface。

然后你必须克隆这些服务。记不清了,太久了搞忘了


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号