Check if in "dev" mode inside a Controller with Symfony

JeanValjean picture JeanValjean · Oct 21, 2012 · Viewed 30.6k times · Source

When using dev mode with a Symfony2.x application, one usually works in locale. Hence, such function does not works as expected (for instance, try to get the current IP under localhost). This could be a problem, e.g. when one try to use such ip-based web service. Hence, I just want to know how to check inside a controller if the Symfony2 application is running in dev mode or not. In that way one can set the behavior of the controller depending by the mode.

Any idea?

Answer

tolgap picture tolgap · Oct 21, 2012

To get the current environment in a Controller you can use:

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

So you just put that in an if() statement to check if it equals to dev.