PHP code example of digimax / dot-env-editor

1. Go to this page and download the library: Download digimax/dot-env-editor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

digimax / dot-env-editor example snippets


use Digimax\DotEnvEditor\DotEnvEditor;

$envPath = __DIR__ . '/.env';

$editor = new DotEnvEditor(
    $envPath,   // the path to the.env file
    true,       // whether to keep a backup of the .env file before writing
);

// or using the static method
$editor = DotEnvEditor::load($envPath, true);

// set backup directory
$editor->setBackupDir(__DIR__ . '/backups');

// Get all variables
var_dump($editor->all());

// Get a specific variable
echo $editor->get('AUTHOR_NAME');

// Set a variable
$editor->set('AUTHOR_NAME', 'Raziul Islam');

// set multiple variables
$editor->set([
    'AUTHOR_URL' => 'https://raziul.dev',
    'AUTHOR_COUNTRY' => 'Bangladesh',
]);

// Remove a variable
$editor->remove('AUTHOR_URL');

// write back to the file
$editor->write();

DotEnvEditor::load($envPath, true)
    ->setBackupDir(__DIR__ . '/backups')
    ->set([
        'AUTHOR_URL' => 'https://raziul.dev',
        'AUTHOR_COUNTRY' => 'Bangladesh',
    ])
    ->remove('AUTHOR_URL')
    ->write();

use Digimax\DotEnvEditor\DotEnvEditor;

public function register(): void
{
    $this->app->singleton(DotEnvEditor::class, function () {
        return DotEnvEditor::load(base_path('.env'))
            ->setBackupDir(storage_path('env-backups')) // backup directory
            ->setBackupCount(5); // only keep latest 5 backup
    });
}

public function update(DotEnvEditor $envEditor)
{
    // Perform form/data validation

    // save the changes
    $envEditor
        ->set([
            'AUTHOR_URL' => 'https://raziul.dev',
            'AUTHOR_COUNTRY' => 'Bangladesh',
        ])
        ->write();
}