On line 12 of the index.php
script file that Symfony version 5 created when I ran the symfony new <project> --full
command, shown below, there is an if-test that when the $_SERVER[ 'APP_DEBUG' ] super global is true
, enables Symfony's Debug class.
use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;
require dirname( __DIR__ ) . '/vendor/autoload.php';
( new Dotenv() )->bootEnv( dirname( __DIR__) . '/.env' );
if( $_SERVER[ 'APP_DEBUG' ] ) {
umask( 0000 );
Debug::enable();
} // End of if( $_SERVER[ 'APP_DEBUG' ] ) ...
$kernel = new Kernel( $_SERVER[ 'APP_ENV' ],
(bool)$_SERVER[ 'APP_DEBUG' ] );
$request = Request::createFromGlobals();
$response = $kernel->handle( $request );
$response->send();
$kernel->terminate( $request, $response );
In the controllers called by the above code, I want to use a similar test to determine when my application is running in debug mode, but I want to know is how and where is $_SERVER[ 'APP_DEBUG' ]
set?
For the question "What effects do the APP_ENV and APP_DEBUG constants have in a Symfony application?" The bootstrap.php can set the $_SERVER[ 'APP_DEBUG' ], but in my case, the bootstrap.php script isn't called and it isn't one of the third-party scripts under vendors.
If it is in $_SERVER[ 'APP_ENV' ], how did it get there?
Note: What effects do the APP_ENV and APP_DEBUG constants have in a Symfony application? appears to suggest that the bootstrap.php script could be setting the debug flag in $_SERVER[ 'APP_ENV' ], but this script isn't part of the installed vendor packages or under the config directory, so isn't called, and so can't have set this flag.
Nigel Ren, offered:
Looks like the .env file (stackoverflow.com/questions/54252210/…).
However, my project's .env
file doesn't have an entry for APP_DEBUG
.
Is there a default value somewhere?
The value used to be set on bootstrap.php
, which came with Symfony <5.1 flex recipes.
Since Symfony 5.1 the file is no longer included on the recipe, and the same is done by the DotEnv
component directly, on the bootEnv()
method (which, by default, is called in the front-controller, either index.php
or console
).
// index.php
(new Dotenv())->bootEnv(dirname(__DIR__) . '/.env');
The doc-block for that method says:
/**
* Loads env vars from .env.local.php if the file exists or from the other .env files otherwise.
*
* This method also configures the APP_DEBUG env var according to the current APP_ENV.
*
* See method loadEnv() for rules related to .env files.
*/
And includes a similar method than what bootstrap.php
used to include:
$k = $this->debugKey;
$debug = $_SERVER[$k] ?? !\in_array($_SERVER[$this->envKey], $this->prodEnvs, true);
$_SERVER[$k] = $_ENV[$k] = (int) $debug || (!\is_bool($debug) && filter_var($debug, \FILTER_VALIDATE_BOOLEAN)) ? '1' : '0';
Basically, if you don't set the value explicitly, it will default to true
to "non-prod" environments, and by default only prod
is a production environment.