简体   繁体   English

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

[英]Symfony: How to call private services in 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 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. 因此,除非您特别需要直接通过$ container-> get()从容器访问服务,否则最佳实践是将服务设为私有。 In fact, the default services.yaml configuration configures all services to be private by default. 实际上,默认的services.yaml配置默认将所有服务配置为私有。

And

https://symfony.com/blog/new-in-symfony-3-4-services-are-private-by-default 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. 在Symfony核心中,我们已经做到了这一点,并且使所有服务和别名变为私有的,除了少数几个在引导时所需的服务和别名。 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*? 因此,我们是否应该放弃将service_container与ContainerAware *一起注入的可能性? That's a possibility that the community might consider when preparing Symfony 5. 社区在准备Symfony 5时可能会考虑这种可能性。

I have an Trait or Abstract controller which have to use by App/Controller. 我有一个必须由App / Controller使用的Trait或Abstract控制器。 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: 特征可以通过自动装配来调用私有服务,而问题是解决的,但Symfony的最佳实践是不要在捆绑软件中仅使用自动装配:

https://symfony.com/doc/current/service_container/autowiring.html 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. 因此,如何在我的Trait或抽象控制器中注入我的私人服务。 Or even in the App controller without the user having to configure are services.yml. 甚至在App控制器中,而无需用户配置的都是services.yml。

I hope I'm clear. 我希望我清楚。

Sorry for my english. 对不起我的英语不好。 I try to improve it ;-) 我试图改善它;-)

If you are developing a third party bundle, ie, a bundle that will be used by other people, then you must define your services manually and also in xml. 如果您正在开发第三方捆绑软件,即将由其他人使用的捆绑软件,那么您必须手动定义服务,也必须使用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. 您正在引用的文章引用了您的AppBundle服务,该服务应该是私有的,因为其中包含您的应用程序/业务逻辑。 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: Api Platform可以在这里进行操作:

routing.xml routing.xml

EntryPointAction.php EntryPointAction.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM