Symfony passing array of arguments to DI services via yml file

三世轮回 提交于 2020-06-25 10:39:32

问题


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?


回答1:


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

services:
  my_di: 
    class: \MyClass
    arguments:
      - { param1: 'myvalue', param2: 'myvalue2' }


来源:https://stackoverflow.com/questions/49814530/symfony-passing-array-of-arguments-to-di-services-via-yml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!