Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony passing array of arguments to DI services via yml file

I'm using symfony 2.x and I have a class which accept and array of configurations from yml file

config.yml

services:
  my_di: 
    class: \MyClass
    arguments:
      - param1: 'myvalue'

MyClass.php

class {

public function __construc(array $configs = []) {

 var_dump($config);

}

Output (this is working correctly)

array (size=1)
   param1 => 'myvalue'
)

But I want to pass one more value to the same array via yml - param2: 'myvalue2'

and the exprected output will be

array (size=1)
   param1 => 'myvalue',
   param2 => 'myvalue2'
)

How can I achieve this?

like image 240
coderex Avatar asked Apr 13 '18 10:04

coderex


2 Answers

Simply use a yaml array in your config.yml file:

services:
  my_di: 
    class: \MyClass
    arguments:
      - { param1: 'myvalue', param2: 'myvalue2' }
like image 187
fxbt Avatar answered Nov 19 '22 08:11

fxbt


You can achieve this with the following syntax (don't put a dash - at the beginning of each row of your associative array):

services:
  my_di: 
    class: \MyClass
    arguments:
      $configs:
        param1: 'myvalue'
        param2: 'myvalue2'
        param3: 'myvalue3'
like image 34
Yoann Kergall Avatar answered Nov 19 '22 08:11

Yoann Kergall