如何在MAMP中设置环境变量?

4

如何在MAMP(版本3.3)中设置环境变量,以便在我的PHP应用程序中使用?

我已经更新了/Applications/MAMP/Library/bin/envvars_和envvars-std_文件,并添加了以下行:

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# envvars-std - default environment variables for apachectl
#
# This file is generated from envvars-std.in
#
if test "x$DYLD_LIBRARY_PATH" != "x" ; then
  DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
else
  DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib"
fi
export DYLD_LIBRARY_PATH

MY_TEST_DETAIL="foo"
export MY_TEST_DETAIL

#

我查看了phpinfo(),但找不到MY_TEST_DETAIL。

请问有人能告诉我哪里出了问题吗?谢谢!


这种方法曾经在MAMP 2.x中有效。 - doublejosh
3个回答

4

我找到了解决方案。在 /Applications/MAMP/conf/apache/httpd.conf 文件中添加以下内容:

SetEnv MY_TEST_DETAIL foo

2
至少对于 MAMP PRO(版本3.5),您可以从菜单中找到并编辑您的Apache httpd.conf文件。
文件 -> 编辑模板 -> Apache -> httpd.conf

MAMP PRO Apache template edit

你可以添加类似这样的内容:
SetEnv ENVIRONMENT development

然后在PHP中,您可以像这样获取环境变量:
$env = getenv('ENVIRONMENT');

if($env == "development") {
   $db_host = "localhost";
}

请注意,如果/当您更新或升级MAMP时,它很可能会用新版本覆盖此httpd.conf文件。

0
# This file is generated from envvars-std.in

我不确定,但可能您没有更改正确的文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接