如何向外部注册的服务提供参数services.yaml
How to provide parameters to service registered outside services.yaml
我遇到了 Symfony 自动装配的问题。
我需要用我自己的替换第三方服务之一。
该服务最初是这样定义的:
ewz_recaptcha.form.type:
class: EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType
public: true
arguments:
- '%ewz_recaptcha.public_key%'
- '%ewz_recaptcha.enabled%'
- '%ewz_recaptcha.ajax%'
- '@ewz_recaptcha.locale.resolver'
- '%ewz_recaptcha.api_host%'
tags:
- { name: form.type }
我的定义看起来很相似:
ewz_recaptcha.form.type:
class: App\Form\Type\EWZRecaptchaType
public: true
arguments:
- ''
- false
- '%ewz_recaptcha.ajax%'
- '@ewz_recaptcha.locale.resolver'
- '%ewz_recaptcha.api_host%'
tags:
- { name: form.type }
我忽略了前 2 个参数,因为它们是稍后从数据库加载的(这是我在服务中所做的更改)。
现在如果我把它放在 config/services.yaml
中就可以正常工作了
但是由于在这个项目中我只将该文件用于项目服务,并且我在第三方配置文件中进行了所有覆盖,所以我尝试将其移动到 config/packages/ewz_recaptcha.yaml
但现在我收到错误提示:
Cannot autowire service "App\Form\Type\EWZRecaptchaType": argument "$publicKey" of method "EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::__construct()" has no type-hint, you should configure its value explicitly.
我什至连接到 Symfony\Component\DependencyInjection\Compiler\AutowirePass
来检查发生了什么,当在 services.yaml
中定义时,$arguments
参数填充了我的参数,但是当移动到 ewz_recaptcha.yaml
参数数组为空。
当服务在 services.yaml 文件外定义时,我是否应该以不同的方式定义参数?
最有可能的问题是,App\Form\Type\EWZRecaptchaType
被意外地注册在两个地方。您在 config/packages/ewz_recaptcha.yaml
中手动执行此操作,此外 config/services.yaml
中的 psr-4 发现也会收集它。
#config/services.yaml
services:
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
解决方法是在此处将表单类型添加到排除的类中,以防止其被注册,例如使用类似的东西:
exclude: '../src/{DependencyInjection,Entity,Form/Type/EWZRecaptchaType.php,Migrations,Tests,Kernel.php}'
我遇到了 Symfony 自动装配的问题。 我需要用我自己的替换第三方服务之一。 该服务最初是这样定义的:
ewz_recaptcha.form.type:
class: EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType
public: true
arguments:
- '%ewz_recaptcha.public_key%'
- '%ewz_recaptcha.enabled%'
- '%ewz_recaptcha.ajax%'
- '@ewz_recaptcha.locale.resolver'
- '%ewz_recaptcha.api_host%'
tags:
- { name: form.type }
我的定义看起来很相似:
ewz_recaptcha.form.type:
class: App\Form\Type\EWZRecaptchaType
public: true
arguments:
- ''
- false
- '%ewz_recaptcha.ajax%'
- '@ewz_recaptcha.locale.resolver'
- '%ewz_recaptcha.api_host%'
tags:
- { name: form.type }
我忽略了前 2 个参数,因为它们是稍后从数据库加载的(这是我在服务中所做的更改)。
现在如果我把它放在 config/services.yaml
中就可以正常工作了
但是由于在这个项目中我只将该文件用于项目服务,并且我在第三方配置文件中进行了所有覆盖,所以我尝试将其移动到 config/packages/ewz_recaptcha.yaml
但现在我收到错误提示:
Cannot autowire service "App\Form\Type\EWZRecaptchaType": argument "$publicKey" of method "EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType::__construct()" has no type-hint, you should configure its value explicitly.
我什至连接到 Symfony\Component\DependencyInjection\Compiler\AutowirePass
来检查发生了什么,当在 services.yaml
中定义时,$arguments
参数填充了我的参数,但是当移动到 ewz_recaptcha.yaml
参数数组为空。
当服务在 services.yaml 文件外定义时,我是否应该以不同的方式定义参数?
最有可能的问题是,App\Form\Type\EWZRecaptchaType
被意外地注册在两个地方。您在 config/packages/ewz_recaptcha.yaml
中手动执行此操作,此外 config/services.yaml
中的 psr-4 发现也会收集它。
#config/services.yaml
services:
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
解决方法是在此处将表单类型添加到排除的类中,以防止其被注册,例如使用类似的东西:
exclude: '../src/{DependencyInjection,Entity,Form/Type/EWZRecaptchaType.php,Migrations,Tests,Kernel.php}'