如何在Symfony2中将嵌套参数值传递给服务

Vic*_*sky 6 parameters service dependency-injection symfony

例如,在我的services.yml文件中的下一个代码:

parameters:
    social:
        facebook:
            app_id: 123456789
            secret: dkl41a5dw1daw11d1wa135451awwlflaw
        google:
            id: 12548411654

services:
    bw.user.social:
        class: BW\UserBundle\Service\SocialService
        arguments: [%social.facebook%]
Run Code Online (Sandbox Code Playgroud)

此方法不起作用.我可以用这个:

services:
    bw.user.social:
        class: BW\UserBundle\Service\SocialService
        arguments: [%social%]
Run Code Online (Sandbox Code Playgroud)

但这并不是我所需要的.如何在没有谷歌的情况下传递facebook值,这是非常重要的,不是更改参数结构?

map*_*phe 2

你必须这样写你的参数:

parameters:
    social.facebook:
        app_id: 123456789
        secret: dkl41a5dw1daw11d1wa135451awwlflaw
    social.google:
        id: 12548411654
Run Code Online (Sandbox Code Playgroud)

然后你可以使用:

services:
    bw.user.social:
        class: BW\UserBundle\Service\SocialService
        arguments: [%social.facebook%]
Run Code Online (Sandbox Code Playgroud)