Symfony2未定义的服务参数

Symfony2未定义的服务参数,symfony,dependency-injection,Symfony,Dependency Injection,我的服务配置是: <services> <service id="my_service" class="Acme\HelloBundle\Hello"> <argument type="service" id="search" on-invalid="null" /> <argument>%other_bundle.class_name%</argument> </service&g

我的服务配置是:

<services>
    <service id="my_service" class="Acme\HelloBundle\Hello">
        <argument type="service" id="search" on-invalid="null" />
        <argument>%other_bundle.class_name%</argument>
    </service>
</services>

%其他\u捆绑包。类\u名称%
当服务“搜索”未定义时,我得到null(谢谢invalid=“null”)

但若参数%other\u bundle.class\u name%未定义,该怎么办?在本例中,我遇到了错误。任何选项设置为:
%other\u bundle.class\u name%
当参数不存在时,我得到null或空字符串


谢谢

您可以通过expression language实现这一点:

以下是通过表达式注入服务的方法,我相信同样的方法也适用于参数:

# app/config/config.yml
services:
    my_mailer:
        class:        Acme\HelloBundle\Mailer
        arguments:    ["@=parameter('other_bundle.class_name') ?: null"]
我不喜欢它,因为它感觉很脏,但如果你真的需要它,没有人可以争辩;)


希望这能有所帮助……

在现实世界中,是否存在根本不定义参数的场景?是一个参数,可以通过配置覆盖,因此我建议您简单地为其定义一个默认值。当在其他捆绑包中定义了参数且budle未启用时,将根本不定义参数。参数可以被覆盖,这是真的,但当我启用“其他”捆绑包时,使用正确的值早于使用默认参数值的捆绑包,我将获得默认值,所以我不希望只依赖于捆绑包启用的顺序。因此,我正在寻找如何为%other\u bundle.class\u name%中的参数设置defalt emty值的选项。这不起作用:参数条件调用容器上的
getParameter
方法。默认情况下,容器的表达式语言中没有映射方法“`HasParameters',但可以自定义。Jovan这看起来不错,但它来自版本2.4,我使用的是2.3。现在我用:编译器传递解决了这个问题。