1

My service configuration is:

<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>

When service "search" is not defined I get null (thanks on-invalid="null").

But what if parameter %other_bundle.class_name% is not defined in this case I got error. Is any option what set to: <argument>%other_bundle.class_name%</argument> that when parameter not exists I get null or empty string?

Thanks

3
  • Is there any real-world scenario where parameter would not be defined at all? Commented Aug 25, 2015 at 10:08
  • 1
    is a parameters and can be overridden by configuration, so i suggest you to simple define a default value for it. Commented Aug 25, 2015 at 10:10
  • Parameter would not be defined at all when is defined in other bundle and the budle is not enabled. Parameters can be overriden, thats true, but when I enable "Other" bundle with correct value earlier than this bundle with default parameter value I will get default value, so I don't want only rely on order of bundle enable. So I looking for option how set defalt emty value for parameter in <argument>%other_bundle.class_name%</argument> Commented Aug 25, 2015 at 10:22

1 Answer 1

1

You might be able to do it via expression language:

Following the official docs for injecting services via expressions, I believe the same can be applied to parameters:

# app/config/config.yml
services:
    my_mailer:
        class:        Acme\HelloBundle\Mailer
        arguments:    ["@=parameter('other_bundle.class_name') ?: null"]

I don't like it, as it really feels dirty but if you really need it, nobody can argue ;)

Hope this helps...

Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't work: parameter condition call the getParameter method on the container. The method ``hasParametes` is not mapped by default in the expression language for container but can be custom defined.
Jovan this looks good, but it from version 2.4 and I'm using 2.3. I solved it for now with: Compiler Passes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.