mykehowells / dotenv
将 .env 文件加载到项目环境中,使用与 Laravel 相同的函数
1.0.3
2017-01-05 20:12 UTC
Requires
- php: >=5.4.0
- vlucas/phpdotenv: ^2.2
This package is auto-updated.
Last update: 2024-08-27 23:02:26 UTC
README
如果你已经使用过 Laravel 并习惯了像更换 .env 文件一样轻松地设置和切换环境。
通过使用 mykehowells/dotenv
,你现在可以通过 composer 将该功能添加到自己的项目中。
通过 Composer 安装
使用以下命令通过 composer 将 mykehowells/dotenv 添加到您的项目中
composer require mykehowells/dotenv
添加以下代码以检查 .env 文件是否存在,如果存在则导入。最好将此文件存储在任何公开可访问的目录之外,因为它将包含可能敏感的数据 - 例如数据库凭证。
有关该包的更多信息,请查看 vlucas/phpdotenv
的 README。
/*---------------------------------------------------------------------------- | ENV CONFIGURATION |----------------------------------------------------------------------------- | | Check to see if an env file exists at project root, if it does, import | keys and values into putenv( $setting ), otherwise env( $key, $default=null ) | will return default value. | |---------------------------------------------------------------------------*/ load_env( __DIR__ );
然后您可以使用 env( $key, $default = null )
函数开始从 .env 文件中检索数据。