doulubashang6936 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

https://symfony.com/doc/current/service_container/alias_private.html

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.

And

https://symfony.com/blog/new-in-symfony-3-4-services-are-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:

https://symfony.com/doc/current/service_container/autowiring.html

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 ;-)

  • 写回答

1条回答 默认 最新

  • douruduan8812 2018-04-23 21:26
    关注

    If you are developing a third party bundle, i.e., a bundle that will be used by other people, then you must define your services manually and also in xml. Those are the best practices.

    Now, third party bundles can have public services. The article you are referencing refers to your AppBundle services, that should be private because contain your application/bussiness logic. The reason why they are private by default is because you should be using Constructor Dependency Injection to use them, so things are more easily testable.

    Now, in regards to your issue, you should't be creating controllers in third party bundles: it's not good practice. Instead, use the routing to route to a service that will perform the action you need. Api Platform does that, here:

    routing.xml

    EntryPointAction.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵