Moufid Taleb
Moufid Taleb

Reputation: 151

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: '[email protected]'
    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.

Upvotes: 4

Views: 3251

Answers (1)

Muamar Ali
Muamar Ali

Reputation: 155

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.

Upvotes: 1

Related Questions