Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a PHP constant as service argument in Symfony 2? [duplicate]

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.

like image 803
gremo Avatar asked Apr 08 '13 11:04

gremo


1 Answers

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%]
    
like image 93
Touki Avatar answered Oct 03 '22 15:10

Touki