2

I have a small console command where I'd like to read some environment variables, but it does not seem to read the vars from the .env file or the server configs in the console (php file works)

The code

protected function execute(InputInterface $input, OutputInterface $output)
{
    $io = new SymfonyStyle($input, $output);
    $debug = $input->getArgument('debug') === 'y' ? true : false;
    $this->project = $input->getArgument('project');

    $start = new DateTime();

    $debug ? $io->text('<fg=green>Starting upload</>') : null;
    dump(getenv('APP_ENV'));
    dump(getenv('MAILER_USERNAME'));
    die;
    ...
}

Commands to test

php bin/console app:make-backup

Output:

Starting upload
false
false

php bin/console app:make-backup --env=prod

Output:

Starting upload
"prod"
false

php bin/console app:make-backup --env=dev

Output:

Starting upload
"dev"
false

.env File

APP_ENV=dev
MAILER_USERNAME=info@xxx.com

I don't see where I am doing wrong? Issue exists on nginx and apache server, BUT using getenv('MAILER_USERNAME') in any php-file works.

miken32
  • 42,008
  • 16
  • 111
  • 154
PrimuS
  • 2,505
  • 6
  • 33
  • 66
  • When you start your command directly in the shell, with `php bin/console what:ever`, that call to the PHP is not passing in automatically all the environment variables that you have active in the current shell. I had the same problem when trying to trigger the debugger with a `XDEBUG_SESSION=1` env var. In the end I had to do this `XDEBUG_SESSION=1 php bin/console what:ever` - only that will effectively set the wanted environment var for your command. So, in your case `APP_ENV=dev php bin/console what:ever` should do it, like magic. :) – userfuser Oct 09 '22 at 09:44

1 Answers1

6

I think your best bet would be to go ahead and inject the env variables. Trying to access them directly can be a bit tricky.

# config/services.yaml
services:
    App\Command\MyCommand:
        $appEnv:         '%env(APP_ENV)%'
        $mailerUsername: '%env(MAILER_USERNAME)%'

# src\Command\MyCommand
class MyCommand extends Command {
    protected static $defaultName = 'app:mine';

    private $appEnv;
    private $mailerUsername;

    public function __construct($appEnv,$mailerUsername) {
        parent::__construct();
        $this->appEnv = $appEnv;
        $this->mailerUsername = $mailerUsername;
    }
    protected function execute(InputInterface $input, OutputInterface $output) {
        $output->writeln(("My Command " . $this->appEnv . " " . $this->mailerUsername));
    }
}
Cerad
  • 48,157
  • 8
  • 90
  • 92
  • 1
    It says `Cannot autowire service "App\Command\MyCommand": argument "$appEnv" of method "__construct()" is type-hinted "string", you should configure its value explicitly.` Removing the type-hinting throws a similar exception – PrimuS Aug 09 '19 at 15:13
  • See the first part of my answer. You need an entry in config/services.yaml – Cerad Aug 09 '19 at 15:15
  • I did and I changed it to https://stackoverflow.com/a/53099308/1092632 but to no avail... – PrimuS Aug 09 '19 at 15:17
  • 1
    Take a break. Then come back and review your code. What I posted works. You have a simple error someplace. – Cerad Aug 09 '19 at 15:20
  • :) While I agree to that, this works: https://stackoverflow.com/a/54902115/1092632 (the second part with the defaults). I may be a little too tired. Since I agree taht your code should work, I accept your answer anyway and say THANKS – PrimuS Aug 09 '19 at 15:24
  • In Symfony 5 i was able to retrieve the .env parameters directly in the commands with the global $_ENV['APP_ENV'] – Carlos Delgado Jan 15 '20 at 20:39