付费课程, 订阅后即可观看

使用 .env 文件防止涉密信息泄露、同时让框架可以灵活控制

本期Coding10 Laravel 6 视频教程我们学习一下如何让配置系统更加完善,更加安全,更好的保护公钥私钥的私密信息不被泄露,同时还可以让项目的开发可以更灵活的控制,我们使用 .env 文件对这类配置信息进行独立维护和配置

kk 2020.07.05 16:06

列位看官,windows环境下getenv()可能拿不到.env配置项数据,你需要将

$dotenv = Dotenv\Dotenv::createImmutable(base_path());

修改成

$dotenv = Dotenv\Dotenv::createUnsafeImmutable(base_path());

国营 2020.07.06 05:31

感谢朋友们的补充完善,windows我用得比较少,这些我的确没遇到过

黄忠 2020.11.12 05:31

我的php env扩展是"vlucas/phpdotenv": "^5.2"(windows10),使用dotenv = Dotenv\Dotenv::createImmutable(base_path())依然无法获得.env文件中的内容,根据该扩展包github地址找到下面方面:

使用$dotenv = Dotenv\Dotenv::createUnsafeImmutable(base_path())就可以了

注明:Using getenv() and putenv() is strongly discouraged due to the fact that these functions are not thread safe, however it is still possible to instruct PHP dotenv to use these functions. Instead of calling Dotenv::createImmutable, one can call Dotenv::createUnsafeImmutable, which will add the PutenvAdapter behind the scenes. Your environment variables will now be available using the getenv method, as well as the super-globals:

翻译:强烈建议不要使用getenv()和putenv(),因为这些函数不是线程安全的,但是仍然可以指示PHP dotenv使用这些函数。而可以调用Dotenv::createUnsafeImmutable,这将在后台添加PutenvAdapter。您的环境变量现在可以使用getenv方法以及超级全局变量

cheng 2022.04.18 09:35

使用$_ENV['APP_NAME'] 可以获取到数据,但是使用getenv('APP_NAME')却获取不到是什么原因

cheng 2022.04.18 09:43

找到原因了,需要使用 $dotenv = \Dotenv\Dotenv::createUnsafeImmutable(base_path());

国营 2022.04.18 10:06

你玩得好快啊,主要靠自己吧,这些东西也好久不玩,基本遗忘干净了

微信扫码登录