Check if in “dev” mode inside a Controller with Symfony

后端 未结 9 2156
無奈伤痛
無奈伤痛 2021-01-01 08:56

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 th

9条回答
  •  时光取名叫无心
    2021-01-01 09:20

    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 or false argument as the second argument to the AppKernel 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.

提交回复
热议问题