Symfony 4.1组件-依赖注入问题 [英] Symfony 4.1 components - Dependency Injection issues
问题描述
我正在用PHP重构旧的应用程序.
我正在尝试使用Symphony依赖项注入组件将服务注入到控制器(或其他服务)中,但是我不知道如何实现,因为Symphony文档比框架组件更适合使用框架./p>
我已经有自己的内核,其中包含所有服务和控制器的容器(控制器已注册为服务).我的控制器从 symfony/frameworkbundle
扩展了AbstractController.所以我现在唯一能做的就是:
通过 $ this-> container-> get('service_id')
从容器获取服务,但是如果构造函数中的服务将类作为参数
公共函数__constructor(SomeClass $ someClass)
然后我遇到了这个异常:
编译容器时,"App \ V2 \ Service \ TestService"服务或别名已被删除或内联.您应该将其公开,或者停止直接使用容器并改为使用依赖项注入.
如果我更改配置以公开所有服务,则:
函数APP \ V2 \ Service \ TestService :: __ construct()的参数太少,传递了0个且恰好期望1个
我准备了要点,以便更好地了解我在说什么: https://gist.github.com/miedzwin/49bac1cc1d5270d3ba1bfcf700abf864
有人可以帮助我使用Symfony组件(不是Symfony框架)进行DI实施吗?一个好的工作例子就足够了.或者只是将您的评论写在我的要旨上,我会尝试解决此问题.
Symfony 4/5自动装配方式
根据您的问题和评论,我认为您需要做的就是修复自动装配.
Symfony 4向很简单:自动连接所有服务和参数,无需手动设置(如果可能).
要将其应用于您的示例,这将是满足您的需求和Symfony 4的最佳配置:
服务:_defaults:#默认情况下将服务依赖项传递给构造函数自动连线:true#默认添加已知标签(用于命令,事件订阅者等)自动配置:true#使用测试,bin文件和另一个更简单的方法公开:真实#自动将字符串/数组/整数参数装配到构造函数#这可以解决"argument" $ facebookUserId"这样的问题方法"__construct()"的结果没有类型提示,则应显式配置其值.绑定:#$ constructorVariableName:配置中的%parameter%$ facebookUserId:'%facebook_user_id%'APP \ V2 \:资源:"../src/app/V2/*"排除:"../src/app/V2/{Script,Trait}"#symfony服务-您仍然必须手动设置3rd paryt服务Symfony \ Component \ DependencyInjection \ ParameterBag \ ContainerBag:参数:-'@service_container'#...
在哪里继续阅读
- 将参数传递给服务
-
If I change configuration to make all services public, then:
Too few arguments to function APP\V2\Service\TestService::__construct(), 0 passed and exactly 1 expected
I prepare a gist, to have better view what I'm talking about: https://gist.github.com/miedzwin/49bac1cc1d5270d3ba1bfcf700abf864
Can someone help me a bit with DI implementation using Symfony components (not Symfony framework)? Good working example would be enough. Or just please put your remarks to my gist, I try to fix this.
解决方案Symfony 4/5 Autowiring way
From your question and comments I think all you need to do is fix autowiring.
Symfony 4-way is simple: autowire all services and parameters, no manual setup (if possible).
To apply that to your example, this would be the best config to fit your needs and Symfony 4:
services: _defaults: # pass service dependencies to constructors by default autowire: true # add known tags (for commands, event subscribers etc) by default autoconfigure: true # to make using tests, bin files and another simpler public: true # autowiring of string/array/int parameters to constructors # this fixes cases like "argument "$facebookUserId" of method "__construct()" has no type-hint, you should configure its value explicitly" bind: # $constructorVariableName: %parameter% in config $facebookUserId: '%facebook_user_id%' APP\V2\: resource: '../src/app/V2/*' exclude: '../src/app/V2/{Script, Trait}' # symfony services - you still have to setup 3rd paryt services manually Symfony\Component\DependencyInjection\ParameterBag\ContainerBag: arguments: - '@service_container' # ...
Where to Continue Reading
这篇关于Symfony 4.1组件-依赖注入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!