automatix
automatix

Reputation: 14532

How to check the environment using the Symfony console tool?

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?


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).

Upvotes: 20

Views: 44596

Answers (7)

SilentBob
SilentBob

Reputation: 121

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)

Upvotes: 8

max4ever
max4ever

Reputation: 12142

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

 $environment = $GLOBALS["env"];

Upvotes: -1

aga
aga

Reputation: 813

php bin/console about

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

enter image description here

Upvotes: 57

Reynier
Reynier

Reputation: 939

php bin/console debug:container --env-vars

Upvotes: 37

Michał G
Michał G

Reputation: 2292

What about

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

in ContainerAwareCommand?

Upvotes: 7

Yarik
Yarik

Reputation: 47

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

getenv('APP_ENV');

Upvotes: 3

Alvin Bunk
Alvin Bunk

Reputation: 7764

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.

Upvotes: 1

Related Questions