Core\Env::mode
Core\Env::mode(): Core\Env::ModeReturns which mode the program runs in: Core\Env::Mode::Production or Core\Env::Mode::Development.
The mode is set in nvs.toml, with default in the [mode] section. No environment variable changes it, so a setting such as APP_ENV has no effect. When nothing is configured, the mode is Production. A deployment is then the careful one until somebody chooses otherwise.
Use it to decide how much your program shows when something goes wrong, or whether it loads test data. Each mode also changes the default of a few settings, and each of those settings can still be set on its own.
Return value
Returns Core\Env::Mode. The mode as a Core\Env::Mode case. If the configuration does not set a mode, the result is Production, the stricter mode.
Examples
Print the mode
<?nvs// Shows how to print which mode the program runs in.
// `match` gives one text for each of the two modes.string $name = match (Core\Env::mode()) { Core\Env::Mode::Production => 'production', Core\Env::Mode::Development => 'development',};
// Nothing is configured here, so the mode is `Production` and this prints `production`.echo 'mode: ', $name, "\n";mode: productionCache only in production
<?nvs// Shows how to choose a setting from the mode: pages are cached in production,// and not cached while you develop.
int $cacheSeconds = Core\Env::mode() == Core\Env::Mode::Production ? 3600 : 0;
// The mode is `Production` here, so this prints `3600`.echo 'cache pages for ', $cacheSeconds, " seconds\n";cache pages for 3600 secondsWhat a visitor sees when an order fails
<?nvs// Shows how an error message depends on the mode. A developer sees the details.// A visitor on a real deployment sees a short message.
class Orders{ // Returns the text to show when saving an order failed. public static function failureText(RuntimeError $error): tainted string { if (Core\Env::mode() == Core\Env::Mode::Development) { return 'Saving the order failed: ' . $error->message; } return 'Sorry, we could not save your order. Please try again.'; }}
try { throw new RuntimeError('the table orders is locked');} catch (RuntimeError $error) { // The mode is `Production` here, so this prints the short message. echo Orders::failureText($error), "\n";}Sorry, we could not save your order. Please try again.