PHP code example of mifesta / dotenv-editor

1. Go to this page and download the library: Download mifesta/dotenv-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/ */

    

mifesta / dotenv-editor example snippets


"    // other reqire packages
    "mifesta/dotenv-editor": "1.*"
},

'providers' => array(
    // other providers
    Mifesta\DotenvEditor\DotenvEditorServiceProvider::class,
),

'DotenvEditor' => Mifesta\DotenvEditor\Facades\DotenvEditor::class,



namespace YourNamespace;

// your code

use Mifesta\DotenvEditor\Facades\DotenvEditor;

class YourClass
{
    public function yourMethod()
    {
        DotenvEditor::doSomething();
    }
}



namespace App\Http\Controllers;

// your code

use Mifesta\DotenvEditor\DotenvEditor;

class TestDotenvEditorController extends Controller {

    protected $editor;

    public function __construct(DotenvEditor $editor)
    {
        $this->editor = $editor;
    }

    public function doSomething()
    {
        $editor = $this->editor->doSomething();
    }
}

$content = DotenvEditor::getContent(); // Get raw content of file .env in root folder

$file = DotenvEditor::load($filePath, $restoreIfNotFound, $restorePath);

$content = DotenvEditor::getContent();

$lines = DotenvEditor::getLines();

$keys = DotenvEditor::getKeys(); // Get all keys
$keys = DotenvEditor::getKeys(['APP_DEBUG', 'APP_URL']); // Only get two given keys if exists

$keyExists = DotenvEditor::keyExists('APP_URL'); // Return true|false

$value = DotenvEditor::getValue('APP_URL');

$file = DotenvEditor::addEmpty();

$file = DotenvEditor::addComment('This is a comment line');

$file = DotenvEditor::setKey('ENV_KEY'); // Set key ENV_KEY with empty value
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want'); // Set key ENV_KEY with none empty value
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want', 'your-comment'); // Set key ENV_KEY with a value and comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-1'); // Update key ENV_KEY with a new value and keep earlier comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value', null, true); // Update key ENV_KEY with a new value, keep earlier comment and use 'export ' before key name
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-2', '', false); // Update key ENV_KEY with a new value and clear comment

$file = DotenvEditor::setKeys([
    [
        'key'     => 'ENV_KEY_1',
        'value'   => 'your-value-1',
        'comment' => 'your-comment-1',
        'export'  => true
    ],
    [
        'key'     => 'ENV_KEY_2',
        'value'   => 'your-value-2',
        'export'  => true
    ],
    [
        'key'     => 'ENV_KEY_3',
        'value'   => 'your-value-3',
    ]
]);

$file = DotenvEditor::deleteKey('ENV_KEY');

$file = DotenvEditor::deleteKeys(['ENV_KEY_1', 'ENV_KEY_2']); // Delete two keys

$file = DotenvEditor::save();

$file = DotenvEditor::backup();

$backups = DotenvEditor::getBackups();

$latestBackup = DotenvEditor::getLatestBackup();

$file = DotenvEditor::restore(); // Restore from latest backup
$file = DotenvEditor::restore(storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709')); // Restore from other file

$file = DotenvEditor::deleteBackup(storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709'));

$file = DotenvEditor::deleteBackups([
    storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709'),
    storage_path('dotenv-editor/backups/.env.backup_2017_04_11_091552')
]); // Delete two backup file

$file = DotenvEditor::deleteBackups(); // Delete all backup

$file = DotenvEditor::autoBackup(true); // Enable auto backup
$file = DotenvEditor::autoBackup(false); // Disable auto backup

$file = DotenvEditor::load('.env.example')->backup()->setKey('APP_URL', 'http://example.com')->save();
return $file->getKeys();
shell
$ php artisan dotenv:get-backups --help