dongzhong8691
2018-04-13 10:10
浏览 94
已采纳

Symfony通过yml文件将参数数组传递给DI服务

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?

图片转代码服务由CSDN问答提供 功能建议

我正在使用symfony 2.x,我有一个接受yml文件配置的类

config.yml

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

MyClass.php

  class {
 
 \ public function __construc(array $ configs = [])  {
 
 var_dump($ config); 
 
} 
   
 
 

输出(这是正常工作)

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

但我想通过yml -param2将另外一个值传递给同一个数组:'myvalue2' \ n

并且表达的输出将是

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

我如何实现这一目标?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpian4954 2018-04-13 10:14
    已采纳

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

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

相关推荐 更多相似问题