PHP Application Configuration: Overview of Tools and Libraries
In modern PHP development, proper configuration organization is a critically important task. Security, portability, and maintainability of the application depend on it. In this article, we will review popular libraries and approaches that help you flexibly manage the settings of your PHP application.
Why do you need a separate configuration library?
Instead of hardcoded parameters (e.g., database passwords directly in the code), configuration should be:
- External — stored separately from business logic.
- Secure — sensitive data does not end up in the repository.
- Flexible — easily changeable for different environments (dev, staging, production).
Using specialized libraries solves these problems in a standardized way.
1. PHP dotenv (vlucas/phpdotenv)
The most popular library for working with .env files. Perfect for projects where you need to quickly and securely store environment variables.
Installation and Basic Usage
composer require vlucas/phpdotenvCreate a .env file in the project root:
DB_HOST=localhostDB_NAME=mydbDB_USER=rootDB_PASS=secretLoading in the application:
<?phprequire_once __DIR__ . '/vendor/autoload.php';
use Dotenv\\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);$dotenv->load();
$dbHost = $_ENV['DB_HOST']; // or getenv('DB_HOST')echo "Connecting to database: " . $dbHost;Advantages:
- Simplicity — the
.envfile does not end up in Git (added to.gitignore). - Security — passwords and keys are stored locally.
- Compatibility with any framework.
2. Symfony Dotenv (symfony/dotenv)
A component from Symfony for working with environment variables. It features built-in support for variable overriding and caching.
composer require symfony/dotenv<?phpuse Symfony\\Component\\Dotenv\\Dotenv;
$dotenv = new Dotenv();$dotenv->load(__DIR__ . '/.env');
// Automatic loading into $_ENV and $_SERVER$appEnv = $_SERVER['APP_ENV'] ?? 'prod';echo "Environment: " . $appEnv;Difference from phpdotenv: Symfony Dotenv supports syntax with variables inside variables:
DATABASE_URL=mysql://${DB_USER}:${DB_PASS}@${DB_HOST}:3306/${DB_NAME}3. symfony/config — Managing Complex Configurations
For large projects where configuration is stored in YAML, XML, or PHP arrays, the symfony/config component is ideal.
Example of Loading YAML Configuration
composer require symfony/config symfony/yamlFile config.yaml:
database: host: 'localhost' port: 3306 name: 'myapp' user: 'root' pass: 'secret'
app: debug: true locale: 'ru_RU'Loading in PHP:
<?phpuse Symfony\\Component\\Config\\FileLocator;use Symfony\\Component\\Config\\Loader\\DelegatingLoader;use Symfony\\Component\\Config\\Loader\\LoaderResolver;use Symfony\\Component\\Yaml\\Yaml;
$configDirectories = [__DIR__ . '/config'];$locator = new FileLocator($configDirectories);$yamlContents = Yaml::parseFile($locator->locate('config.yaml'));
$dbHost = $yamlContents['database']['host'];echo "DB Host: " . $dbHost;Additional Features:
- Support for importing and inheriting configurations.
- Caching of processed configs.
- Schema validation via
symfony/config-definition.