Ben Guest
Ben Guest

Reputation: 1558

What effects do the APP_ENV and APP_DEBUG constants have in a Symfony application?

What do the various settings for these environment variables do?

My current guess is that APP_ENV is used only for choosing which config to load (config/packages/{APP_ENV}/*) and APP_DEBUG turns on/off additional console output for kernel events, and enables debugging views for exceptions etc.

Is this correct or do these variables do additional things?

Upvotes: 8

Views: 13970

Answers (1)

yivi
yivi

Reputation: 47308

APP_ENV Uses:

Decide which configuration files to load in the configuration directory.

This is accomplished in within your Kernel class.

By default, in the configureContainer() method provided you'll find this line, where $this->environment represents the value of APP_ENV.

$loader->load( $confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob' );

Obviously, you could customize and or change that behaviour according your your needs.

Decide which .env files to load.

In the file config/bootstrap.php you'll see the basic environment initialization. This script uses the DotEnv component to read the .env files

(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

And this component will use the value of APP_ENV to search for these. If you take a look at DotEnv::loadEnv() you will see this:

if (file_exists($p = "$path.$env")) {
    $this->load($p);
}

if (file_exists($p = "$path.$env.local")) {
    $this->load($p);
}

Which means that for a given APP_ENV set as dev it will try to load first .env.dev and then .env.dev.local.

To set the value of APP_DEBUG, if it's not explicitly set.

If you do not set APP_DEBUG, it will be set by bootstrap.php according to the value of APP_ENV. This can be seen clearly here on this line:

$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV'];

(If $_SERVER['APP_DEBUG'] is available use that, if not try to use $_ENV['APP_DEBUG'], and if that is not set either set it true if APP_ENV is not prod).

APP_DEBUG Uses:

This constant is used on entry points scripts for the application (public/index.php or bin/console), where you would see these two uses:

To initialize error reporting and error handling globally. This is done using Debug::enable():

if ( $_SERVER['APP_DEBUG'] ) {
    umask( 0000 );

    Debug::enable();
}

The method Debug::enable() is simple and easy to understand: It changes error_reporting, sets displays_errors, and registers error and exception handlers.

To initialize Kernel for the application.

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);

This sets the %kernel.debug% (Kernel::isDebug()) parameter. This can value can be used by many packages to make decisions about what to do when the system is deemed to be in debugging mode. There is no mandated behaviour, each module can use the information in the most suitable manner for the module use-case.

Upvotes: 13

Related Questions