简体   繁体   中英

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.

What is the proper way to inject this array of services as parameter ?

Here is the services.yaml part:


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:

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:

Cannot autowire service "App\\Mailer\\RestMailer": argument "$parameters" >of method "__construct()" is type-hinted "array", you should configure >its value explicitly.

Did you already configure in the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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