.env 文件不会将变量加载到 config.php - 学生设置第一个 php 开发环境 [英] .env file wont load variables into config.php - student setting up first php dev environment

查看:19
本文介绍了.env 文件不会将变量加载到 config.php - 学生设置第一个 php 开发环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午,

我是一名学习计算机科学的学生,我正在尝试重新创建我朋友用来托管他们基于 PHP 的网络应用程序的环境.他们在 Mac 上使用 heroku local(Procfile 调用 heroku-php-apache2)来设置他们的环境.我在 Windows 10 PC 上,从我所做的研究来看,heroku local 不受任何支持.因此,我启用了 WSL 安装的 Ubuntu 18.04 和 Apache2,据我所知,下载并安装了使其运行所需的所有其他组件(composer、modrewrite、modenv 等).phpinfo()、heroku 的示例项目以及我正确显示的任何简单的 php 页面.另一方面,我朋友的应用程序仍然给我带来麻烦.

I'm a student studying Computer Science and I'm trying to recreate the environment my friend is using to host their PHP based web app. They're on a Mac using heroku local (Procfile calling heroku-php-apache2) to set up their environment. I'm on a Windows 10 PC, and from what research I'm done, heroku local is not supported in any way. So I enabled WSL installed Ubuntu 18.04 and Apache2 and, as far as I can tell, downloaded and installed all of the other components necessary to make it run (composer, modrewrite, modenv, etc). phpinfo(), heroku's sample project, and any simple php pages I make display properly. My friend's app on the other hand is still giving me trouble.

他们使用 .env 来声明项目特定的环境变量,这些变量在 config.php 中进一步定义.该应用程序已在 Heroku 和他们的机器上部署并运行,但是当我尝试在我的机器上本地加载该应用程序时,我收到一个异常,表示未加载环境变量.如果我添加local: php -S localhost:80"到 Procfile 并在 Ubuntu 上本地运行 heroku,它看到 .env 文件并说它已加载,只是为了回退我的 apache2 实例抛出的相同错误.

They're using a .env to declare project specific environment variables that are further defined in a config.php. The app is deployed and works in Heroku and on their machine, but when I try to load the app locally on my machine I get an exception thrown saying the environment variables aren't being loaded. If I add "local: php -S localhost:80" to the Procfile and run heroku local on Ubuntu, it sees the .env file and says its loaded, only to kick back the same errors my apache2 instance is throwing.

可能是什么原因造成的?我已经编辑了 php.ini 以包含一个E",启用了 modrewrite 和 modenv,确保我的 .env 文件是用 UTF-8 编码的 - 我已经广泛搜索了一个可能会发生的原因,但我保持走到了死胡同.有没有关于heroku local"的东西?我缺少的命令和实例?我对 php、Web 服务器和一般编程仍然不熟悉,因此任何有关为什么我的 .env 文件不起作用的相关信息或我可以使用 WSL 让 heroku local 在 Ubuntu 上工作的任何可能方法都将不胜感激.

What could be causing this? I've edited php.ini to include an "E", enabled modrewrite and modenv, made sure my .env file was encoded in UTF-8 - I've searched far and wide for a reason this might be happening but I keep coming to a dead end. Is there something about the "heroku local" command and instance that I'm missing? I'm still new to php, web servers, and programming in general, so any relevant information regarding why my .env file isn't working or any possible ways I can get heroku local to work on Ubuntu using WSL would be massively appreciated.

推荐答案

Straightforward Method

您可以通过将环境变量添加到您的 .bashrc 文件中来非常简单地做到这一点,假设您的 Windows 环境变量设置正确并且安装了 WSL,我将使用 Java 作为例如,但任何环境变量都可以使用.

Straightforward Method

You can do this very simply by adding the environment variable to your .bashrc file, assuming that your Windows Environment variables are set correctly and WSL is installed, I'm going to use Java as an example, but any environment variable will work.

在 WSL 中使用文本编辑器或从 WSL 主页键入 code .bashrc 以初始化 WSL VSCode 编辑器.将环境变量添加到文件中:

Use text editor in WSL or type code .bashrc from WSL home to initialize the WSL VSCode editor. Add the environment variable to the file:

# Shared environment variables
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10

只需确保将目录更改为指向您的目录.就我而言,它在 D:\Java\jdk11.0.4_10 中,在 WSL 中是 /mnt/d/Java/jdk11.0.4_10

Just ensure that you change the directory to point to your directory. In my case, it's in D:\Java\jdk11.0.4_10 which in WSL is /mnt/d/Java/jdk11.0.4_10

此外,由于您使用的是 Windows 二进制文件,因此在从 WSL bash shell 运行时必须指定文件类型:

Also, since you're using Windows binaries, you must specify the file type when running from a WSL bash shell:

Windows:

javac MyClass.java
java MyClass

WSL:

javac.exe MyClass.java
java.exe MyClass

请注意,正在使用 .exe 文件扩展名,因为我们正在调用 Windows 二进制文件.如果它是 JDK 的本机 Linux 发行版,您可以简单地使用 java 命令.

Note that the .exe file extension is being used, since we're calling the Windows binary. If it was a native Linux distribution of the JDK you could simply use java command.

这适用于通过 WSL 运行的任何 Windows 二进制文件.

This holds true for any Windows binary that is being run through WSL.

这篇关于.env 文件不会将变量加载到 config.php - 学生设置第一个 php 开发环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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