简体   繁体   English

如何在Symfony服务中注入参数数组?

[英]How to inject an array of parameters in Symfony service?

I'm trying to inject some twig views as templates for a custom mailer service, which will be used as dependency by another service. 我试图注入一些树枝视图作为自定义邮件服务的模板,该服务将被另一个服务用作依赖项。

I don't get why, but it's like Symfony doesn't see the parameters I'm trying to inject in $parameters. 我不知道为什么,但是就像Symfony没看到我要在$ parameters中注入的参数。

What is the proper way to inject this array of services as parameter ? 将这个服务数组作为参数注入的正确方法是什么?

Here is the services.yaml part: 这是services.yaml部分:


parameters:
    locale: 'en'
    template: '%fos_user.registration.confirmation.template%'
    resetting: '%fos_user.resetting.email.template%'
    from_email: 'somemail@mail.com'
    confirmation: '%fos_user.registration.confirmation.from_email%'
    resetting_password: '%fos_user.resetting.email.from_email%'

services:

    user.mailer.rest:
        class: App\Mailer\RestMailer
        public: false
        parent: fos_user.mailer.twig_swift
        autoconfigure: false
        autowire: true
        arguments:
            $parameters:
              - '@template'
              - '@resetting'

The RestMailer service constructor: RestMailer服务构造函数:

public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
  {
    $this->mailer = $mailer;
    $this->router = $router;
    $this->twig = $twig;
    $this->parameters = $parameters;
  }

  public function sendConfirmationEmailMessage(UserInterface $user)
  {
    $template = $this->parameters['template']['confirmation'];
//...

Here is the returned error: 这是返回的错误:

In DefinitionErrorExceptionPass.php line 54: 在DefinitionErrorExceptionPass.php第54行中:

Cannot autowire service "App\\Mailer\\RestMailer": argument "$parameters" >of method "__construct()" is type-hinted "array", you should configure >its value explicitly. 无法自动装配服务“ App \\ Mailer \\ RestMailer”:方法“ __construct()”的参数“ $ parameters”带有类型提示的“数组”,您应显式配置其值。

Did you already configure in the services.yml? 您是否已经在services.yml中进行了配置?

If you want to use the parameters in config.yml, you have to set up in the services.yml to used/call the parameters. 如果要使用config.yml中的参数,则必须在services.yml中进行设置以使用/调用参数。

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

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