Managing environment variables is crucial for configuring applications across different environments, from development to production. Today, we’re excited to introduce cleaniquecoders/php-env-key-manager, a new PHP package that makes managing environment variables easier and more flexible.
php-env-key-manager allows you to set, enable, or disable environment keys directly in your .env file across any PHP application. Whether you’re working in Laravel, Symfony, CodeIgniter, or a custom PHP project, this package provides a straightforward way to manage configuration.
The .env file holds sensitive information and configurations specific to your environment, such as database credentials, API keys, and debug settings. However, adding, updating, or toggling keys manually can be tedious and error-prone, especially in large projects. php-env-key-manager simplifies this by providing a set of easy-to-use methods that automate these tasks.
Install the package via Composer:
composer require cleaniquecoders/php-env-key-manager
Using php-env-key-manager is straightforward. Here’s how you can set, disable, and enable keys in your .env file.
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager; // Path to your .env file $envFilePath = __DIR__ . '/.env'; $envManager = new EnvKeyManager($envFilePath); // Set a key $envManager->setKey('APP_DEBUG', 'true'); // Disable a key $envManager->disableKey('APP_DEBUG'); // Enable a key $envManager->enableKey('APP_DEBUG');
With these methods, you can quickly update environment configurations without manually editing the .env file.
Here’s how you can integrate php-env-key-manager in popular PHP frameworks.
In Laravel, you can register EnvKeyManager as a singleton in the AppServiceProvider to make it available throughout your application.
In AppProvidersAppServiceProvider:
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager; public function register() { $this->app->singleton(EnvKeyManager::class, function ($app) { return new EnvKeyManager($app->environmentFilePath()); }); }
Create a Laravel Artisan command to set, disable, or enable environment keys:
<?php namespace App\Console\Commands; use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager; use Illuminate\Console\Command; class ManageEnvKeyCommand extends Command { protected $signature = 'env:manage-key {action} {key} {value?}'; protected $description = 'Manage an environment key'; protected $envManager; public function __construct(EnvKeyManager $envManager) { parent::__construct(); $this->envManager = $envManager; } public function handle() { $action = $this->argument('action'); $key = $this->argument('key'); $value = $this->argument('value'); switch ($action) { case 'set': $this->envManager->setKey($key, $value); $this->info("Key {$key} set to {$value}."); break; case 'disable': $this->envManager->disableKey($key); $this->info("Key {$key} has been disabled."); break; case 'enable': $this->envManager->enableKey($key); $this->info("Key {$key} has been enabled."); break; default: $this->error("Invalid action. Use 'set', 'disable', or 'enable'."); } } }
To use EnvKeyManager in Symfony, initialize it with the .env path and use it in Symfony commands or services.
composer require cleaniquecoders/php-env-key-manager
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager; // Path to your .env file $envFilePath = __DIR__ . '/.env'; $envManager = new EnvKeyManager($envFilePath); // Set a key $envManager->setKey('APP_DEBUG', 'true'); // Disable a key $envManager->disableKey('APP_DEBUG'); // Enable a key $envManager->enableKey('APP_DEBUG');
In CodeIgniter, you can initialize EnvKeyManager with the .env path and use it within controllers.
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager; public function register() { $this->app->singleton(EnvKeyManager::class, function ($app) { return new EnvKeyManager($app->environmentFilePath()); }); }
<?php namespace App\Console\Commands; use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager; use Illuminate\Console\Command; class ManageEnvKeyCommand extends Command { protected $signature = 'env:manage-key {action} {key} {value?}'; protected $description = 'Manage an environment key'; protected $envManager; public function __construct(EnvKeyManager $envManager) { parent::__construct(); $this->envManager = $envManager; } public function handle() { $action = $this->argument('action'); $key = $this->argument('key'); $value = $this->argument('value'); switch ($action) { case 'set': $this->envManager->setKey($key, $value); $this->info("Key {$key} set to {$value}."); break; case 'disable': $this->envManager->disableKey($key); $this->info("Key {$key} has been disabled."); break; case 'enable': $this->envManager->enableKey($key); $this->info("Key {$key} has been enabled."); break; default: $this->error("Invalid action. Use 'set', 'disable', or 'enable'."); } } }
For more details, visit the GitHub repository: cleaniquecoders/php-env-key-manager.
This package simplifies environment management, allowing you to quickly toggle, add, or remove settings without directly editing .env files. We hope it brings ease to your development workflow. Give it a try and let us know your feedback!
Photo by Luke Chesser on Unsplash
The above is the detailed content of Introducing PHP Env Manager: Simplify Environment Management in PHP Applications. For more information, please follow other related articles on the PHP Chinese website!