dev-resources.site
for different kinds of informations.
Introducing PHP Env Manager: Simplify Environment Management in PHP Applications
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.
Why php-env-key-manager
?
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.
Key Features
-
Set Key-Value Pairs: Easily add or update environment variables with
setKey
. -
Enable Keys: Uncomment environment keys with
enableKey
. -
Disable Keys: Comment out environment keys with
disableKey
. - Framework-Agnostic: Use this package in any PHP project.
- Framework Integrations: Get dedicated usage examples for Laravel, Symfony, and CodeIgniter.
Installation
Install the package via Composer:
composer require cleaniquecoders/php-env-key-manager
Basic Usage
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.
Framework-Specific Usage
Here’s how you can integrate php-env-key-manager
in popular PHP frameworks.
Laravel Integration
In Laravel, you can register EnvKeyManager
as a singleton in the AppServiceProvider
to make it available throughout your application.
- Register as a Singleton
In App\Providers\AppServiceProvider
:
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
public function register()
{
$this->app->singleton(EnvKeyManager::class, function ($app) {
return new EnvKeyManager($app->environmentFilePath());
});
}
- Use in an Artisan Command
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'.");
}
}
}
Symfony Integration
To use EnvKeyManager
in Symfony, initialize it with the .env
path and use it in Symfony commands or services.
-
Initialize
EnvKeyManager
with Symfony’s.env
path.
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
$envFilePath = __DIR__ . '/../../.env';
$envManager = new EnvKeyManager($envFilePath);
- Create a Symfony Command
<?php
namespace App\Command;
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ManageEnvKeyCommand extends Command
{
protected static $defaultName = 'env:manage-key';
private $envManager;
public function __construct(EnvKeyManager $envManager)
{
parent::__construct();
$this->envManager = $envManager;
}
protected function configure()
{
$this
->setDescription('Manage an environment key')
->addArgument('action', InputArgument::REQUIRED, 'Action: set, disable, enable')
->addArgument('key', InputArgument::REQUIRED, 'The environment key')
->addArgument('value', InputArgument::OPTIONAL, 'The value for set action');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$action = $input->getArgument('action');
$key = $input->getArgument('key');
$value = $input->getArgument('value');
switch ($action) {
case 'set':
$this->envManager->setKey($key, $value);
$output->writeln("Key {$key} set to {$value}.");
break;
case 'disable':
$this->envManager->disableKey($key);
$output->writeln("Key {$key} has been disabled.");
break;
case 'enable':
$this->envManager->enableKey($key);
$output->writeln("Key {$key} has been enabled.");
break;
default:
$output->writeln("Invalid action. Use 'set', 'disable', or 'enable'.");
return Command::FAILURE;
}
return Command::SUCCESS;
}
}
CodeIgniter Integration
In CodeIgniter, you can initialize EnvKeyManager
with the .env
path and use it within controllers.
-
Initialize
EnvKeyManager
in your controller.
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
$envFilePath = ROOTPATH . '.env';
$envManager = new EnvKeyManager($envFilePath);
- Controller Method for Managing Environment Keys
<?php
namespace App\Controllers;
use CleaniqueCoders\PhpEnvKeyManager\EnvKeyManager;
class EnvController extends BaseController
{
protected $envManager;
public function __construct()
{
$this->envManager = new EnvKeyManager(ROOTPATH . '.env');
}
public function manageKey($action, $key, $value = null)
{
switch ($action) {
case 'set':
$this->envManager->setKey($key, $value);
return "Key {$key} set to {$value}.";
case 'disable':
$this->envManager->disableKey($key);
return "Key {$key} has been disabled.";
case 'enable':
$this->envManager->enableKey($key);
return "Key {$key} has been enabled.";
default:
return "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
Featured ones: