PHP Application Configuration: Overview of Libraries and Tools
Proper configuration organization is one of the key aspects of modern PHP application development. The security, flexibility, and maintainability of your code depend on how you store and load settings. In this article, we will review the main libraries and approaches for managing configuration in PHP, including working with environment variables, structured configs, and DI containers.
Why is a separate configuration system needed?
Storing settings in hardcoded constants or directly in code is an anti-pattern. The main problems with this approach are:
- Inability to change application behavior without rewriting code;
- Leakage of sensitive data (passwords, API keys) into the repository;
- Confusion between environments (dev, staging, production);
- Difficulty in testing and reusing components.
Popular Libraries for Configuration
1. PHP Dotenv (vlucas/phpdotenv)
The most popular library for loading environment variables from a .env file. It is ideal for small and medium-sized projects, especially when using frameworks like Laravel or Symfony.
<?phprequire 'vendor/autoload.php';
use Dotenv\\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);$dotenv->load();
// Now you can use $_ENV or getenv()$dbHost = $_ENV['DB_HOST'] ?? 'localhost';$dbName = getenv('DB_NAME');Key Features:
- Automatic loading of variables from
.env; - Support for required variables (
required()); - Filtering and customization of parsing;
- Security: the
.envfile should not be committed to Git.
2. Symfony Config Component
A powerful component from Symfony for working with complex hierarchical configurations. Supports YAML, XML, PHP files, and even databases.
<?phpuse Symfony\\Component\\Config\\FileLocator;use Symfony\\Component\\Config\\Loader\\DelegatingLoader;use Symfony\\Component\\Config\\Loader\\LoaderResolver;use Symfony\\Component\\Yaml\\Yaml;
// Loading YAML configuration$config = Yaml::parseFile(__DIR__ . '/config/database.yaml');
// Using FileLocator to find files$locator = new FileLocator([__DIR__ . '/config']);$yamlFile = $locator->locate('app.yaml');
// Advanced loading with resolver$loader = new DelegatingLoader(new LoaderResolver([ new YamlFileLoader($locator), new PhpFileLoader($locator)]));$config = $loader->load('config.yml');Advantages:
- Configuration validation based on schemas (TreeBuilder);
- Support for inheritance and merging of configs;
- Flexible loader system;
- Caching of processed configs for speed.
3. PHP-DI (PHP Dependency Injection Container)
Although PHP-DI is primarily a DI container, it provides excellent tools for configuration through service and parameter definitions. It allows you to set settings directly in code or through PHP files.
<?phpuse DI\\ContainerBuilder;
$builder = new ContainerBuilder();$builder->addDefinitions([ 'db.host' => getenv('DB_HOST') ?: 'localhost', 'db.port' => 3306, PDO::class => function ($c) { return new PDO( "mysql:host={$c->get('db.host')};port={$c->get('db.port')}", getenv('DB_USER'), getenv('DB_PASS') ); }]);
$container = $builder->build();$pdo = $container->get(PDO::class);Features:
- Defining parameters and services in one place;
- Support for definition files (PHP, YAML via ext