jackiedo/dotenv-editor
Edit Laravel .env files safely with a fluent API: read raw content and parsed entries, add/update/delete keys, manage comments and export flags, insert empty/comment lines, check key existence, and create/restore backups with configurable storage.
Installation:
composer require jackiedo/dotenv-editor
Publish the config file (optional):
php artisan vendor:publish --provider="JackieDo\DotenvEditor\DotenvEditorServiceProvider" --tag="config"
First Use Case:
Access the editor via the provided route (default: /dotenv-editor):
php artisan route:list | grep dotenv-editor
Navigate to the URL in your browser to edit .env variables interactively.
Key Files:
config/dotenv-editor.php (for customization)routes/web.php (ensure the route is included if not auto-discovered)Environment-Specific Edits:
Use the DotenvEditor facade to programmatically modify .env files:
use JackieDo\DotenvEditor\Facades\DotenvEditor;
DotenvEditor::set('APP_ENV', 'local');
DotenvEditor::save();
Validation Hooks:
Extend validation logic by binding to the dotenv-editor.saving event:
DotenvEditor::extend(function ($editor) {
$editor->validate(function ($key, $value) {
if ($key === 'DB_PASSWORD' && strlen($value) < 8) {
throw new \InvalidArgumentException('Password must be at least 8 characters.');
}
});
});
Multi-Environment Management:
Use the DotenvEditor::load() method to switch between .env files dynamically:
DotenvEditor::load('.env.staging');
API Access: Expose the editor via an API endpoint for headless applications:
Route::post('/api/env', function (Request $request) {
DotenvEditor::set($request->key, $request->value);
DotenvEditor::save();
return response()->json(['success' => true]);
});
Seeding Defaults:
Populate .env with defaults during deployment:
DotenvEditor::setDefault('APP_DEBUG', env('APP_ENV') !== 'production');
File Permissions:
Ensure the .env file is writable by the web server user:
chmod 644 .env
Debugging: Check Laravel logs for Permission denied errors.
Caching Issues:
Clear config cache after manual .env edits:
php artisan config:clear
Hidden Characters:
Avoid copying .env values from rich-text editors (e.g., Word, Slack). Use plain text editors or pbcopy/pbpaste.
Route Conflicts:
The default route (/dotenv-editor) may conflict with other packages. Rename it in config/dotenv-editor.php:
'route' => [
'path' => 'admin/env',
'middleware' => ['web', 'auth'],
],
Sensitive Data Exposure:
Never commit .env to version control. Use .env.example as a template and .gitignore to exclude .env.
Log Changes:
Enable debug mode in config/dotenv-editor.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Validate Syntax:
Use DotenvEditor::validate() to check for malformed key-value pairs:
try {
DotenvEditor::validate();
} catch (\InvalidArgumentException $e) {
report($e);
}
Backup .env:
Create a backup before bulk edits:
cp .env .env.bak-$(date +%Y%m%d)
Custom Storage:
Override the default .env path by binding a custom DotenvEditor implementation:
$this->app->bind('jackiedo/dotenv-editor', function () {
return new \JackieDo\DotenvEditor\CustomDotenvEditor('/path/to/custom.env');
});
GUI Themes:
Extend the Blade view (resources/views/vendor/dotenv-editor/index.blade.php) to customize the UI (e.g., dark mode, monospace fonts).
Encryption: Integrate with Laravel's encryption services to secure sensitive values:
DotenvEditor::extend(function ($editor) {
$editor->encrypting = true;
});
Webhook Triggers: Dispatch events on save to trigger external actions (e.g., restart services):
DotenvEditor::extend(function ($editor) {
$editor->onSave(function () {
\Artisan::call('queue:restart');
});
});
How can I help you explore Laravel packages today?