7

When defining services using the configuration file, how can I pass a PHP constant (CURLAUTH_DIGEST in this example) as a constructor argument?

I can't test it right now but I assume that:

services:
    my_service:
        class: "%my_service.class%"
        arguments: [CURLAUTH_DIGEST]

Wouldn't work because CURLAUTH_DIGEST is converted to a string.

0

1 Answer 1

13

Here's a way to do this

  1. Add a line in your config to include a .php configuration

    app/config/config.yml

    imports:
        - { resource: constants.php }
    
  2. Create a new file constants.php

    app/config/constants.php

    <?php
    
    $container->setParameter('curlauth.digest', CURLAUTH_DIGEST);
    
  3. You can now access this constant in your service

    @Bundle/Resources/config/services.yml

    services:
        my_service:
            class: "%my_service.class%"
            arguments: [%curlauth.digest%]
    
1
  • Thank you, this shows us how to do it in YAML. The duplicate answer (I didn't read very well) states that it's not possible to do it (only in XML). I voted to close, now I'll do for reopen.
    – gremo
    Commented Apr 9, 2013 at 10:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.