2018-04-23 19:48
浏览 80

Symfony:如何在Bundle Controller中调用私有服务

I creat a bundle and I would like use best practice with it.

So all my services are private

So unless you specifically need to access a service directly from the container via $container->get(), the best-practice is to make your services private. In fact, the default services.yaml configuration configures all services to be private by default.


In Symfony core we've already done that and we made all services and aliases private, except a few selected ones, that are required at bootstrap time. In fact, bootstrapping is the last and only legitimate use case for using the container directly.

So, should we deprecate the possibility to inject the service_container entirely alongside with ContainerAware*? That's a possibility that the community might consider when preparing Symfony 5.

I have an Trait or Abstract controller which have to use by App/Controller. The trait can call the private service with autowiring and the probleme is fix BUT The best practice of Symfony is don't use only autowiring in Bundle:

Public and Reusable Bundles¶

Public bundles should explicitly configure their services and not rely on autowiring.

So How to inject my private service in my Trait or abstract controller. Or even in the App controller without the user having to configure are services.yml.

I hope I'm clear.

Sorry for my english. I try to improve it ;-)

图片转代码服务由CSDN问答提供 功能建议



https: //

因此,除非您特别需要直接从 容器访问服务 通过$ container-> get(),最佳做法是将您的 服务设为私有。 实际上,默认的services.yaml配置 默认将所有服务配置为私有。

< a href =“”rel =“nofollow noreferrer”> / new-in-symfony-3-4-services-private-private-by-default

在Symfony核心中,我们已经完成了 我们使所有服务和 别名为私有,除了一些选定的,在 bootstrap时需要。 实际上,bootstrapping是直接使用容器的最后也是唯一合法的用例。

那么,我们是否应该弃用将完全注入 service_container与ContainerAware *一起注入的可能性? 这是社区在准备Symfony时可能考虑的一种可能性。

我有一个必须由App / Controller使用的Trait或Abstract控制器。 特征可以通过自动装配调用私有服务,问题是修复但是Symfony的最佳实践是不要仅使用Bundle中的自动装配: \ n



那么如何在我的Trait或抽象控制器中注入我的私人服务。 或者甚至在没有用户配置的App控制器中都是services.yml。


对不起我的英语 。 我尝试改进它; - )

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题