Check if in "dev" mode inside a Controller with Symfony
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
.
As of Symfony 2.5 it could be done as:
$this->container->get('kernel')->getEnvironment();
Directly asking Kernel of it's environment looks nicer than searching for parameter.
Since you want to know if you are in dev mode (not necessarilly the environment named "dev"), you can retrieve the kernel from the service container and check the isDebug
method return:
$kernel = $this->get('kernel');$devMode = $kernel->isDebug();
As noted in the documentation (emphasis is mine),
Important, but unrelated to the topic of environments is the
true
orfalse
argument as the second argument to theAppKernel
constructor. This specifies if the application should run in "debug mode". Regardless of the environment, a Symfony application can be run with debug mode set to true or false. This affects many things in the application, such as displaying stacktraces on error pages or if cache files are dynamically rebuilt on each request. Though not a requirement, debug mode is generally set to true for the dev and test environments and false for the prod environment.Internally, the value of the debug mode becomes the kernel.debug parameter used inside the service container.