ctorh23/dotenv
Composer 安装命令:
composer require ctorh23/dotenv
包简介
Loads environment variable definitions from a file into the $_ENV superglobal.
README 文档
README
Dotenv parses files containing user-defined environment variables and populates the $_ENV superglobal with the resulting values.
Installation
Dotenv requires PHP 8.3 or newer.
composer require ctorh23/dotenv
Usage
Basic Example
use Ctorh23\Dotenv\Dotenv; $dotenv = new Dotenv(__DIR__); $dotenv->load();
In the code snippet above, you provide a directory containing your .env file. It may also contain .env.local, .env-environment_specific, and .env-environment_specific.local files. These will be parsed in the following order:
- .env
- .env.local
- .env-environment_specific
- .env-environment_specific.local
If the same variable is declared in multiple files, the last one will overwrite its definition in the previous files. environment_specific could be, for example, 'development', 'testing', 'production', etc. Which environment_specific file will be parsed depends on the special variable APP_ENV, which must be declared in either .env or .env.local. You can change the name of this variable:
$dotenv->setAppEnvName('APPLICATION_ENVIRONMENT');
By default, Dotenv doesn't overwrite existing environment variables. This includes variables defined in the shell or by the web server. This means that if EXAMPLE_VAR=example-value has already been defined in the shell, it will not be overwritten, even if it is defined in one of the .env files. This behavior can be changed:
$dotenv->setOverwrite(true);
You can also provide the path to the .env files using the setPath method instead of a constructor argument:
$dotenv->setPath(__DIR__);
If your file is not named .env, but instead my_vars, for example, you can provide a path to that file:
$dotenv = new Dotenv(); $dotenv->setPath(__DIR__ . '/my_vars') ->load();
Then Dotenv will parse the following files:
- my_vars
- my_vars.local
- my_vars-environment_specific
- my_vars-environment_specific.local
Finally, you can use environment variables in your application by calling the getVar static method:
$myVar = Dotenv::getVar('myvar'); $anotherVar = Dotenv::getVar('non-existent', 'default_value'); //The second argument is a default value in case a variable does not exist.
Alternative Usage
You can choose not to use the default logic. You can provide a single file to the Dotenv object:
$dotenv = new Dotenv(); $vars = $dotenv->processFile(__DIR__ . '/custom-vars');
Or you can provide a list of files:
$dotenv = new Dotenv(); $vars = $dotenv->processFileList([ __DIR__ . '/custom-vars', __DIR__ . '/custom-vars.development', ]);
Then, you have to load the parsed variables into the $_ENV superglobal:
$dotenv->writeVars($vars);
Syntax of .env Files
Environment variable definitions must follow this format:
VAR=value
# This is a comment
ANOTHER_VAR="2nd value"
- The name of a variable must begin with an alphabetic character or an underscore, followed by alphanumeric characters or underscores.
- Any character is accepted for the value, but escape sequences ('\n', '\t', '\r', etc.) must be preceded by a backslash.
- Lines starting with
#are ignored and not parsed.
ctorh23/dotenv 适用场景与选型建议
ctorh23/dotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ctorh23/dotenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ctorh23/dotenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-28