如何在laravel或php中动态更改php dotenv(.env)变量? [英] How to change php dotenv (.env) variables dynamically in laravel or php?

查看:75
本文介绍了如何在laravel或php中动态更改php dotenv(.env)变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这样的东西:

env('APP_ENV');
setenv('APP_ENV', 'testing');
env('APP_ENV');

输出:

staging
testing

我找到了一个答案如何进行更改Laravel中的.env文件中的动态变量?但是.env在这里永久保存,我不想永久保存.phpunit是怎么做到的?因为我可以这样输入phpunit.xml:

I find one answer How to change variables in the .env file dynamically in Laravel? but here .env is saved permanently, I don't want to save permanently. How phpunit is doing this ? Because I can put in phpunit.xml this :

...
<php>
    <env name="APP_ENV" value="testing"/>
</php>
....

然后env('APP_ENV')给我'测试'...

And env('APP_ENV') gives me 'testing'...

推荐答案

putenv()就像一种魅力:

putenv() work like a charm :

echo env('APP_ENV');
putenv('APP_ENV=testing');
echo env('APP_ENV');

输出:

staging
testing

.env文件未附加...

.env file is unattached ...

这篇关于如何在laravel或php中动态更改php dotenv(.env)变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆