Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Symfony 4 application, how do I pass an environment variable to a service class?

I am creating an app using Symfony 4 and Docker. In my .env file, I have the following line:

DEVICE_CREATION_SECRET=123456

... and in my services.yaml file, I have the following definition:

VMS\Application\DigitalRetail\Handler\DeviceForwardHandler:
    arguments:
        - env(DEVICE_CREATION_SECRET)

... which I expect to hand off my secret (123456) to my class, since I have this in that class:

public function __construct(string $deviceCreationSecret)
{
    $this->deviceCreationSecret = $deviceCreationSecret;
}

But when I run my app and dump out the value, I get env(DEVICE_CREATION_SECRET) rather then my secret (123456). What do I need to get access to that secret?

like image 980
Patrick Avatar asked Dec 13 '22 12:12

Patrick


1 Answers

I think this way should work:

VMS\Application\DigitalRetail\Handler\DeviceForwardHandler:
    arguments:
        - '%env(DEVICE_CREATION_SECRET)%'

https://symfony.com/doc/current/configuration/external_parameters.html

like image 150
Flash Avatar answered Dec 17 '22 07:12

Flash