Nathan Page
Contributed by Nathan Page in #60139

The Runtime component lets you run PHP/Symfony applications on different runtimes like PHP-FPM, FrankenPHP, Swoole, and more, without changing anything in your application code. It allows you to create front controllers in a runtime-agnostic way.

In Symfony apps, the default front controller looks like this thanks to the Runtime component:

1
2
3
4
5
6
7
8
// public/index.php
use App\Kernel;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context): Kernel {
    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

The Runtime component integrates seamlessly with the Dotenv component to load environment variables for configuration. You can configure runtime behavior either by setting the APP_RUNTIME_OPTIONS environment variable or by adding options under extra.runtime in your composer.json:

1
2
3
4
5
6
7
8
9
10
{
    "require": {
        "...": "..."
    },
    "extra": {
        "runtime": {
            "dotenv_path": "/path/to/some/.env"
        }
    }
}

In Symfony 7.3, we've introduced a new configuration option to load multiple .env files in your front controller. The new option, called dotenv_extra_paths, accepts an array of additional .env files to load. You can also combine it with dotenv_overload: true if you want later files to overwrite previously loaded variables.

For example, you can dynamically load different environment files based on the current cloud region:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// public/index.php
use App\Kernel;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

$_SERVER['APP_RUNTIME_OPTIONS'] = [
    'dotenv_path' => \sprintf('envs/%s.env', $_SERVER['APP_ENV'] ?? 'local'),
    'dotenv_extra_paths' => [
        \sprintf('envs/%s-%s.env', $_SERVER['APP_ENV'] ?? 'local', $_SERVER['AWS_REGION']),
    ],
    'dotenv_overload' => true,
];

return function (array $context): Kernel {
    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

With this new feature, Symfony simplifies complex environment setups for modern deployments.

Published in #Living on the edge