Search code examples
phpsymfonydependency-injectioncontrollercontainers

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


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?


Solution

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