saff-elli-khan / dotenv
动态环境变量和配置管理库。
dev-master
2020-03-29 22:38 UTC
Requires
- php: ^7.1
This package is auto-updated.
Last update: 2024-09-15 03:34:28 UTC
README
动态环境变量和配置管理库。
语言
PHP
特性
- 获取环境变量
- 解析ENV文件
- 创建/更新/删除变量
- 验证变量
Composer安装
composer require saff-elli-khan/dotenv
使用方法
这是一个非常容易使用的库!
use Dotenv\Dotenv\Dotenv;
use Dotenv\DotenvExceptions\DotenvExceptions;
require_once 'vendor/autoload.php';
try {
//File Path In First Parameter.
//Setting Parameter 2 To True Automatically Generates The ENV File If Not Exists (Default False).
//Setting Parameter 3 To False Disables Exceptions And The Library Returns False On Error (Default True).
$dotenv = new Dotenv(".env", true);
$dotenv->load();
} catch (DotenvExceptions $e) {
print_r($e->getMessage());
}
方法
获取
$dotenv->get($variableName); //Return Data
getenv($variableName); //Alternative
$_ENV[$variableName]; //Alternative
创建/更新
//Parameter 1 Is An Array Containing Variable Names And Values.
//Parameter 2 Is An Optional Comment String.
//Will Update A Variable If Already Exists.
//Method ->put() Is Required To Be Called To Commit The Action.
$dotenv->addVariables(["Foo" => "Bar"], "A test comment")->put();
删除
//Parameter 1 Is An Array Containing Variable Names To Be Removed.
//Method ->put() Is Required To Be Called To Commit The Action.
$dotenv->removeVariables(["Foo", "Moo"])->put();
验证
//Call ->options() Method First To Initialize Validations.
//Parameter 1 To ->required() Method Is An Array Containing Variable Names That Are Required.
$dotenv->options()->required(["Foo", "Moo"]);
高级验证
//Call A Third Method On ->required() Method Like is_email() To Validate All Required Variables
$dotenv->options()->required(["Foo", "Moo"])->is_email();
验证方法
- is_ip();
- not_empty();
- is_empty();
- is_match($regex);
- is_url();
- is_userName();
- is_contact(false); //如果只想进行整数验证,请设置为True
- is_email();
- is_string();
- is_numuric();
- is_integer();
- is_float();
对于任何类型的错误,请告知我们的看法。谢谢!