18

I use the Symfony CLI tool and I'm looking for a way to check the environment. I could not find anything like

$ php bin/console get environment

Is there another way for that? Is it possible / How to find out, which environment is currently active?


EDIT

The application is running in a concrete environment (and using then the app.php for prod or an app_{env} for any other environment).

So what I actually want to achieve is to find out, which environment is currently being used by the application (when the app is called via HTTP, e.g. in a browser).

automatix
  • 14,018
  • 26
  • 105
  • 230

7 Answers7

51
php bin/console about

This will display info about your app, one row will concern Environment:

enter image description here

aga
  • 753
  • 6
  • 10
28
php bin/console debug:container --env-vars
Dharman
  • 30,962
  • 25
  • 85
  • 135
Reynier
  • 790
  • 9
  • 20
  • 5
    this solution is better than first one. for example it saw `DATABASE_URL` which wasn't visible with `php bin/console about`. – quant2016 Aug 05 '20 at 11:32
7

What about

$this->getContainer()->getParameter('kernel.environment');

in ContainerAwareCommand?

Blue
  • 22,608
  • 7
  • 62
  • 92
Michał G
  • 2,234
  • 19
  • 27
  • 3
    `ContainerAwareCommand` has been deprecated in 4.2 and should be used anymore, so this solution shouldn't be used. https://symfony.com/blog/new-in-symfony-4-2-important-deprecations#deprecated-containerawarecommand – Brian F Mar 11 '19 at 21:25
  • You can use `ParameterBagInterface` injected as a dependency instead. – SteveB Oct 15 '19 at 14:36
4

As for Symfony that uses Flex you can get APP_ENV variable via getenv() PHP function:

getenv('APP_ENV');
Yarik
  • 49
  • 2
3

I know the question has been asked a long time ago, but if it could be useful for womeone, in Sf5.4 (at least, maybe Sf6+ too), you could use

php bin/console debug:dotenv

to have all the variables set by .env files (and so, the APP_ENV too)

SilentBob
  • 51
  • 4
2

Just typing in:

php bin/console

will show you all the commands available. I think normally by default, both 'dev' and 'prod' are available. You can specify commands specific to an environment by using the option --env=ENV.

So to debug the routing in dev you would enter:

php bin/console --env=dev debug:router

and it would show routes in the dev environment. I'm not sure if that helps you or not.


EDIT #2

You could add a test to you Twig file and print out your environment:

<p>Application Environment: {{ app.environment }}</p>

This is a quick and easy solution.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • Thank you for the fast answer! But I mean something else: The application is running in a concrete environment (and using then the `app.php` for `prod` or an `app_{env}` for any other environment). So how to find out, which environment is currently be use by the application, when (when the app is called via a HTTP request, e.g. in a browser)? – automatix Feb 08 '17 at 22:18
  • You should probably update your post with that particular info. – Alvin Bunk Feb 08 '17 at 22:23
  • I added an EDIT #2 section - take a look to see if that helps or not. – Alvin Bunk Feb 08 '17 at 23:13
0

When you can't get the container injected in any way, as a last resort you can try

 $environment = $GLOBALS["env"];
max4ever
  • 11,909
  • 13
  • 77
  • 115