Comment on page
Laravel EnvClient
Manage and validate environmental variables with artisan console commands, environmental rules and facades
composer require lionix/envclient
- To construct the
\Lionix\Envclient
use Laravel Service Container instead of plain construct.
Signature | Description |
env:get {key} | Prints .env variable value |
env:set {key} {value} | Sets .env variable if validation rules are passed |
env:check | Check all env variables for validness |
env:empty | Print empty .env variables |
make:envrule {name} | Create a new .env validation rules |
Set an environment variable using
env:set
artisan command.php artisan env:set EXAMPLE_ENV_VARIABLE 'example value'
The command will modify your environment file by replacing or adding the given key to it.
If you want to apply validation rules to environmental variables before
env:set
command will modify the file you will have to publish command package configuration files.php artisan vendor:publish --provider='Lionix\EnvClient\Providers\EnvClientServiceProvider' --tag='config'
The command will create
config/env.php
return [
/**
* Validation classes which contain environment rules
* applied by env artisan commands.
*
* Add your validation classes created by
* `php artisan make:envrule` command to apply their rules
*
* @var array
*/
'rules' => [
\App\Env\BaseEnvValidationRules::class
]
];
and
app/Env/BaseEnvValidationRules.php
namespace App\Env;
use Lionix\EnvValidator;
class BaseEnvValidationRules extends EnvValidator
{
/**
* Validation rules that apply to the .env variables.
*
* @return array
*/
public function rules() : array
{
return [
//
];
}
}
By adding validation rules into
rules
method return value you will apply them to env:set
command....
public function rules() : array
{
return [
'DB_CONNECTION' => 'required|in:mysql,sqlite'
];
}
...
This way if you try to set an invalid value to the
DB_CONNECTION
variable with env:set
command, the console will print out an error$ php artisan env:set DB_CONNECTION SomeInvalidValue
The selected DB_CONNECTION is invalid.
If your environment file was modified you can run
env:check
command which will check all variables for validness and print out the results.$ php artisan env:check
The selected DB_CONNECTION is invalid.
By default, the script will generate a class in
App/Env
namespace.php artisan make:envrule DatabaseEnvRules
app/Env/DatabaseEnvRules.php
namespace App\Env;
use Lionix\EnvValidator;
class DatabaseEnvRules extends EnvValidator
{
/**
* Validation rules that apply to the .env variables.
*
* @return array
*/
public function rules() : array
{
return [
//
];
}
}
...
public function rules() : array
{
return [
'DB_CONNECTION' => 'requried|in:mysql,sqlite,pgsql,sqlsrv'
'DB_HOST' => 'requried',
'DB_PORT' => 'requried|numeric',
'DB_DATABASE' => 'requried',
'DB_USERNAME' => 'requried',
'DB_PASSWORD' => 'requried'
];
}
...
You can add the
DatabaseEnvRules
class to env.php
configuration file at the rules
key. That way all the rules specified in the class will affect package artisan commands.config/env.php
return [
/**
* Validation classes which contain environment rules
* applied by env artisan commands.
*
* Add your validation classes created by
* `php artisan make:envrule` command to apply their rules
*
* @var array
*/
'rules' => [
\App\Env\BaseEnvValidationRules::class
\App\Env\DatabaseEnvRules::class // <- our database rules
]
];
Or you can use
Lionix\EnvClient
Facade to validate the input with given validation rules:...
$client = app()->make(\Lionix\Envclient::class);
$client->useValidator(new \App\Env\DatabaseEnvRules())->update($databaseCredentials);
if ($client->errors()->isNotEmpty()) {
// handle errors
} else {
// success, the variables are updated
}
...
- protected $getter : Lionix\EnvClient\Interfaces\EnvGetterInterface
- protected $setter : Lionix\EnvClient\Interfaces\EnvSetterInterface
- protected $validator : Lionix\EnvClient\Interfaces\EnvValidatorInterface
void
: __construct() Create a new instance of EnvClient using default dependenciesself
: useGetter(Lionix\EnvClient\Interfaces\EnvGetterInterface $getter) Set client getter dependencyself
: useSetter(Lionix\EnvClient\Interfaces\EnvSetterInterface $setter) Set setter dependencyself
: useValidator(Lionix\EnvClient\Interfaces\EnvValidatorInterface $validator) Set validator dependency merging current errors with the validator errorsarray
: all() Get all env variables from the environmental filebool
: has(string $key) Check if the environmental file contains the keymixed
: get(string $key) Get the env variable using the key (returns the output ofIlluminate\Support\Env
get method)self
: set(array $values) Set the environmental variables at runtime if validation rules passedself
: save() Save previously set variables to the environmental fileself
: update() If validation rules passed then set and save variables to the environmental filebool
: validate(array $values) Check values validness and retrieve passed statusIlluminate\Support\MessageBag
: errors() Get all validation errors occurred during the class lifetime
void
: __construct() Create a new instance of EnvGettermixed
: get(string $key) Get the env variable using the key (returns the output ofIlluminate\Support\Env
get method)array
: all() Get all env variables from the environmental filebool
: has(string $key) Check if the environmental file contains the key
- protected $variablesToSet : array
void
: __construct() Create a new instance of EnvSettervoid
: set(array $values) Merge given values with variablesToSet propertyvoid
: save() Save all variables previously set by the set method to the environmental file- protected
string
: sanitize(string $value) Sanitize input values
- protected $errors : Illuminate\Support\MessageBag
void
: __construct() Create a new instance of EnvValidatorarray
: rules() Returns class validation rulesbool
: validate(array $values) Validate given valuesIlluminate\Support\MessageBag
: errors() Get validator errorsvoid
: mergeErrors(Illuminate\Support\MessageBag $errors) Merge given MessageBag with current errors
Last modified 2yr ago