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

时间:2018-04-13 10:10:01

标签: php symfony dependency-injection yaml symfony-2.3

我正在使用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'

,表达的输出将是

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

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

只需在config.yml文件中使用yaml数组:

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

答案 1 :(得分:-1)

另一种可能性是执行以下操作:

class DetailScreen extends StatelessWidget {
    return MaterialApp(
      home: Scaffold(
        body: DefaultTabController(
          length: 2,
          child: CustomScrollView(
            physics: BouncingScrollPhysics(),
            slivers: <Widget>[
              SliverAppBar(
                bottom: TabBar(
                  unselectedLabelColor: Colors.white,
                  labelColor: Colors.black,
                  indicatorSize: TabBarIndicatorSize.label,
                  indicator: BoxDecoration(
                    color: Colors.white,
                    borderRadius: new BorderRadius.only(
                      topLeft: const Radius.circular(20),
                      topRight: const Radius.circular(20),
                    ),
                  ),
                  tabs: [
                    Tab(
                      child: Container(
                        decoration: BoxDecoration(
                            borderRadius: new BorderRadius.only(
                          topLeft: const Radius.circular(20.0),
                          topRight: const Radius.circular(20.0),
                        )),
                        child: Align(
                          alignment: Alignment.center,
                          child: Text(
                            "Recycle",
                          ),
                        ),
                      ),
                    ),
                    Tab(
                        child: Container(
                      width: MediaQuery.of(context).size.width * 0.5,
                      decoration: BoxDecoration(
                          borderRadius: new BorderRadius.only(
                        topLeft: const Radius.circular(20.0),
                        topRight: const Radius.circular(20.0),
                      )),
                      child: Align(
                        alignment: Alignment.center,
                        child: Text("Upcycle"),
                      ),
                    ))
                  ],
                ),
                expandedHeight: 250.0,
                pinned: false,
                floating: false,
                backgroundColor: Colors.transparent,

                onStretchTrigger: () {
                  // Function callback for stretch
                  return;
                },
                flexibleSpace: FlexibleSpaceBar(
                  //blur image when dragged down
                  stretchModes: <StretchMode>[
                    StretchMode.zoomBackground,
                    StretchMode.blurBackground,
                    StretchMode.fadeTitle,
                  ],
                  centerTitle: true,
                  //title
                  title: Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Text(
                        '$imagename',
                        style: TextStyle(
                            fontSize: 40.0,
                            fontFamily: 'Rajdhani',
                            fontWeight: FontWeight.w700,
                            color: Colors.white),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  ),
                  background: Container(
                    decoration: MyBoxDecoration(imagename),
                  ),
                ),
              ),
              SliverPadding(...),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

}