This package offers you a possibility to edit your .env dinamically, for example out of a controller.
Right now, the package offers the following functionalities:
- Change values from your .env
- Create backups of your .env
The following functions are in process:
- Restore a previous version of your .env
- Add new entries
- Delete entries
Already tested with:
- Laravel 5.2
Add the package to your composer.json require-dev
section:
"brotzka/laravel-dotenv-editor": "dev-master"
and run
composer update
Or you may require the package via:
composer require brotzka/laravel-dotenv-editor
Add the following line to your config/app.php
providers:
Brotzka\DotenvEditor\DotenvEditorServiceProvider::class,
Add the following line to your config/app.php
aliases:
'DotenvEditor' => Brotzka\DotenvEditor\DotenvEditorFacade::class,
Finally you have to publish the config file via:
php artisan vendor:publish
Now you can edit the config file and put in your values.
The following example shows an controller with a method, in which we change some values from the .env. Make sure, the entries you want to change, really exist in your .env.
namespace App\Http\Controllers;
use Brotzka\DotenvEditor\DotenvEditor;
class EnvController extends Controller
{
public function test(){
$env = new DotenvEditor();
$env->changeEnv([
'TEST_ENTRY1' => 'one_new_value',
'TEST_ENTRY2' => $anotherValue,
]);
}
}
Before editing the .env, a backup will be created (by default in resources/backups/dotenv-editor
).
/**
* Returns the current backup-path
*
* @return mixed
*/
/**
* Set a new backup-path.
* The new directory will be created if it doesn't exist
*
* @param $path
* @return bool
*/
/**
* Checks, if a given key exists in your .env-file.
* Returns false or true
*
* @param $key
* @return bool
*/
/**
* Returns the value matching to a given key.
* Returns false, if key does not exist.
*
* @param $key
* @return bool|mixed
*/
/**
* Used to create a backup of the current .env.
* Will be assigned with the current timestamp.
*
* @return bool
*/
/**
* Returns the timestamp of the latest version.
*
* @return int|mixed
*/
/**
* Restores the latest backup or a backup from a given timestamp.
* Restores the latest version when no timestamp is given.
*
* @param null $timestamp
* @return bool|string
*/
/**
* Returns an array with all available backups.
* $formated on 0 will show the timestamps instead of date time format.
* Returns false, if no backups were found.
*
* @param int $formated
* @return array|bool
*/
/**
* Returns filename and path for the given timestamp
*
* @param $timestamp
* @return bool|string
*/
/**
* Returns the content of a given backup file
* or the content of the current env file.
*
* @param null $backup
* @return array
*/
/**
* Writes the content of a env file to an array.
*
* @param $file
* @return array
*/
/**
* Saves the given data to the .env-file
*
* @param $array
* @return bool
*/
/**
* Change the given values of the current env file.
*
* @param array $data
* @return bool
*/
/**
* Add data to the current env file.
* Data will be placed at the end.
*
* @param array $data
* @return bool
*/
/**
* Delete one or more entries from the env file.
*
* @param array $data
* @return bool
*/