简体   繁体   English

如何从 .env 文件中获取键的值?

[英]How to get the value of a key from the .env file?

In the .env file there are settings of key values;.env文件中有键值的设置; for example: APP_ENV=prod例如: APP_ENV=prod

How to get the value of the key APP_ENV for example?例如,如何获取键APP_ENV的值?

See https://github.com/symfony/dotenv / https://symfony.com/doc/4.1/components/dotenv.html , which parses into $_ENV , and PHP has a getenv function which you use to get the value out of. See https://github.com/symfony/dotenv / https://symfony.com/doc/4.1/components/dotenv.html , which parses into $_ENV , and PHP has a getenv function which you use to get the value在......之外。

use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');

$appEnv = getenv('APP_ENV');
// you can also use ``$_ENV`` or ``$_SERVER``

You can use the Dotenv() class as the documentation describe here you can call the APP_ENV like this您可以使用Dotenv() class 作为此处描述的文档,您可以像这样调用APP_ENV

use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load('..\.env');
$appEnv= $_ENV['APP_ENV'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM