Installation:
composer require vizrex/laravel-env-switcher
Publish the package config (if needed):
php artisan vendor:publish --provider="Vizrex\EnvSwitcher\EnvSwitcherServiceProvider"
First Use Case:
Switch to the dev environment:
php artisan env:switch dev
Verify the .env file reflects the dev configuration, and check for .env.dev.active (the previous active file).
php artisan env:switch {new_env} [--force]
{new_env}: dev, prod, or testing.--force: Override safety checks (e.g., uncommitted changes)..env (active environment).env.{env}.active (previously active environment).env.{env} (environment-specific configs, auto-created from .env.example if missing).Local Development:
dev and testing frequently:
php artisan env:switch dev # For feature development
php artisan env:switch testing # For test runs
--force to bypass warnings (e.g., during CI/CD):
php artisan env:switch prod --force
Environment-Specific Configs:
DB_PASSWORD) in .env.dev/.env.prod.APP_DEBUG=true) per environment.CI/CD Integration:
# Example: Deploy to staging
php artisan env:switch testing
php artisan migrate --env=testing
Backup/Restore:
.env to .env.backup manually before switching.env:switch to revert, then manually restore .env.backup if needed.Git Ignore:
Add these to .gitignore to avoid committing environment files:
.env
.env*.active
.env.dev
.env.prod
.env.testing
Custom Environments:
Extend the package by modifying the EnvSwitcherServiceProvider:
config/env-switcher.php:
'environments' => [
'dev', 'prod', 'testing', 'staging'
],
switchEnv() logic in EnvSwitcherCommand.php to handle new files.Pre-Switch Hooks: Add validation before switching (e.g., check for uncommitted changes):
// In EnvSwitcherCommand.php
protected function prepareSwitch()
{
if (!app()->environment('local') && !$this->option('force')) {
$this->error('Switching environments is disabled in non-local environments. Use --force to override.');
exit(1);
}
}
Post-Switch Actions: Chain commands after switching (e.g., restart queues):
php artisan env:switch prod && php artisan queue:restart
File Permissions:
.env files are writable:
chmod -R 775 storage bootstrap/cache
sudo chown -R $USER:$USER .
Missing .env.example:
.env.example is missing, the package will fail to create new environment files..env to .env.example or create it manually:
cp .env .env.example
Active File Conflicts:
.env.{env}.active exists but .env is missing, the package may behave unpredictably..env from backup or recreate it:
cp .env.example .env
Caching Issues:
php artisan config:clear
php artisan cache:clear
Dry Run:
Test the switch without modifying files by inspecting the package’s logic in EnvSwitcherCommand.php:
// Temporarily add logging:
protected function copyEnvFile($source, $destination)
{
$this->info("Copying: {$source} -> {$destination}");
file_put_contents($destination, file_get_contents($source));
}
Check File States:
Use ls -la to verify file states:
ls -la .env* # Look for `.active` files or missing configs
Environment Detection: Debug current environment with:
php artisan env
Or check Laravel’s environment:
// In a Tinker session:
app()->environment();
Alias the Command:
Add an alias to ~/.bashrc or ~/.zshrc for faster access:
alias envswitch='php artisan env:switch'
Then use:
envswitch dev
Environment-Specific .gitignore:
Use a template to auto-generate .gitignore entries for all environments:
echo ".env\n.env*.active\n.env.dev\n.env.prod\n.env.testing" > .gitignore
Backup Strategy: Automate backups before switching:
#!/bin/bash
cp .env .env.backup-$(date +%Y%m%d)
php artisan env:switch $1
Custom Naming:
Rename environment files to match your project’s conventions (e.g., .env.local):
getEnvFilePath() method in EnvSwitcherCommand.php:
protected function getEnvFilePath($env)
{
return base_path(".env.{$env}");
}
Force Flag Safety:
Use --force sparingly. Instead, handle edge cases in the prepareSwitch() method:
if (file_exists('.env') && md5_file('.env') !== md5_file('.env.example')) {
$this->warn('Warning: .env has customizations. Use --force to proceed.');
}
How can I help you explore Laravel packages today?