prinx/dotenv
Composer 安装命令:
composer require prinx/dotenv
包简介
Make easy access to the environment variables in your application
关键字:
README 文档
README
PHP Dotenv
Get easily access to environment variables.
Installation
Open a command prompt into your project root folder and run:
composer require prinx/dotenv
Usage
Quick start
// Require composer autoload file if it has not been done yet. require_once __DIR__ . '/path/to/vendor/autoload.php'; /* * Retrieve an environment variable. Returns null if variable not found. */ $hostname = env('DEV_DB_HOST'); /* * Retrieve an environment variable. Returns default value passed as second argument if variable not found */ $port = env('DEV_DB_PORT', 3306); /* * Add a variable to the current loaded environment (will not save in the .env file) */ addenv('LOG_LEVEL', 'info'); /* * Wrtie variable to the env file. * Will also automatically load the variable into the current environment. * If the file already contains the variable, the variable will be overwritten. */ persistenv('LOG_LEVEL', 'warn'); persistenv('LOG_LEVEL', 'debug'); persistenv('LOG_LEVEL', 'info'); /* * Get all environment variables */ env() // OR allenv();
Writing a .env file
The .env file format will be:
VARIABLE_NAME=value
For example:
SESSION_DRIVER=file DEV_DB_HOST=localhost DEV_DB_PORT=3306 PROD_DB_HOST=prod_db_ip PROD_DB_PORT=3308
As standard, the variable name is capital letter with underscores to separate words.
Comments
You can write comments in your .env file by preceding the comment by a hash (#).
Example:
# Supported: file|database SESSION_DRIVER=file
Types of values
By default, env variable will be retrieved as string, except booleans, and null.
String
You can use quotes to surround strings.
APP_NAME=My app # or APP_NAME="My app" DB_HOST=173.0.0.0 # or DB_HOST="173.0.0.0"
Boolean
The values true, "true" or 'true', will be got as the boolean true.
The values false, "false" or 'false' will be got as the boolean false.
# Will be got as a boolean true APP_DEBUG=true # Will be got as a boolean false APP_DEBUG=false
Same as:
APP_DEBUG="true" APP_DEBUG="false"
Null
The values null, "null" or 'null', will be got as null.
# Will be got as a null APP_DEBUG=null APP_DEBUG="null"
Referring to another variable
You can refer to the value of another variable in your .env file by putting the name of the variable you are referring to variable inside ${}:
# .env SESSION_DRIVER=mysql MESSAGE=App based on ${SESSION_DRIVER} database
// PHP echo env('MESSAGE'); // App based on mysql database
Loading a specific .env file
By default, the package automatically look for the .env file in the project root folder. But you can load the env file from anywhere by using the loadenv function:
// Require composer autoload file if it has not been done yet. require_once __DIR__ . '/path/to/vendor/autoload.php'; loadenv('/path/to/somewhere/.env'); // Then everything goes as usual $apiKey = env('API_KEY');
Using the Dotenv instance
You can also get or set a variable using the Dotenv class instance:
The Dotenv instance can be accessed by calling the dotenv() function:
$dotenv = dotenv();
Getting a variable
$hostname = dotenv()->get('DEV_DB_HOST'); // With a default value $hostname = dotenv()->get('DEV_DB_HOST', 'localhost');
Getting all variables
$hostname = dotenv()->all(); // or use get without any parameter $hostname = dotenv()->get();
Adding a variable to the current loaded environment
dotenv()->add('GUEST_NAME', 'john');
Writing a variable to the .env file
dotenv()->persist('GUEST_NAME', 'john');
Create your own Dotenv instance
You can create your own Dotenv instance just by using the Dotenv class:
use Prinx\Dotenv; $dotenv = new Dotenv('path/to/.env'); $dotenv->get('VARIABLE');
Contributing
- Give a star to the repo ☺️
- Fork the repo.
- Correct a bug, add a new feature.
- Write tests.
- Create a pull request.
License
prinx/dotenv 适用场景与选型建议
prinx/dotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「env」 「dotenv」 「phpdotenv」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 prinx/dotenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 prinx/dotenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 prinx/dotenv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A tool to edit phpdotenv files
A wrapper with a fluent interface for new and old versions of vlucas/phpdotenv or symfony/dotenv, providing a common interface to easily read values from .env files
Enable env variables to Kirby
An environment variable convenience library extension for vlucas/phpdotenv
Laravel dotenv manager
Loads environment variables from `.env` to `getenv()`, `$_ENV`
统计信息
- 总下载量: 3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 13
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-27