通过虚拟主机配置设置 Application_ENV 并在 PHP 中读取它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2378871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:14:51  来源:igfitidea点击:

Set Application_ENV via virtual host config and read this in PHP

phpapachezend-frameworkconfiguration

提问by reedwolf

I like how this works in Zend Framework. I can know which environment I'm currently using by checking APPLICATION_ENV constant in my controller.

我喜欢它在 Zend Framework 中的工作方式。通过检查控制器中的 APPLICATION_ENV 常量,我可以知道我当前使用的是哪个环境。

<VirtualHost *:80>
    #ServerName 
    #DocumentRoot

        SetEnv APPLICATION_ENV "development"

    # Directory
</VirtualHost>

But unfortunately I can't use ZF in my current project. How can I check this environment variable in my PHP code?

但不幸的是我不能在我当前的项目中使用 ZF。如何在我的 PHP 代码中检查这个环境变量?

采纳答案by Gordon

Since SetEnvset's the value to Apache's environment, you can get it with

由于SetEnv设置了 Apache 环境的值,因此您可以使用

or just

要不就

  • getenv— Gets the value of an environment variable
  • getenv— 获取环境变量的值


If you look at public/index.phpin a ZF project, you will see ZF uses getenv:

如果您查看public/index.phpZF 项目,您将看到 ZF 使用getenv

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? 
                                  getenv('APPLICATION_ENV') : 
                                  'production'));


An often use alternative would be to read the Hostname from PHP and define the constant accordingly:

一个经常使用的替代方法是从 PHP 读取主机名并相应地定义常量:

if(!defined('APPLICATION_ENV')) {
    if(FALSE === stripos($_SERVER['SERVER_NAME'], 'www.yourdomain.com')) {
        define(APPLICATION_ENV, 'development');
    } else {
        define(APPLICATION_ENV, 'production');
    }
}

This way, you don't have to rely on the environment setting at all.

这样,您根本不必依赖环境设置。

回答by Pascal MARTIN

SetEnvdefines an environment variable.

SetEnv定义一个环境变量。

Once this has been set (either in your Apache's configuration, or at the system level), you can read its value using the getenvfunction :

一旦设置(在您的 Apache 配置中,或在系统级别),您就可以使用以下getenv函数读取其值:

echo getenv('APPLICATION_ENV');


For instance, if you use this in your .htaccessfile :


例如,如果您在.htaccess文件中使用它:

SetEnv TEST glop

You can use this portion of PHP code :

您可以使用这部分 PHP 代码:

var_dump(getenv('TEST'));

And you'll get :

你会得到:

string 'glop' (length=4)

回答by O_o lovesCandy

you can also access it from the $_SERVERvariable.

您也可以从$_SERVER变量访问它。

回答by Firman Syah

I had the same problem then I solved it. The way to solve the problem is to declare all variables in an apache init script.

我遇到了同样的问题,然后我解决了它。解决问题的方法是在 apache init 脚本中声明所有变量。

I'm using apache on centos and the init script is located in /etc/init.d/httpd

我在 centos 上使用 apache 并且初始化脚本位于 /etc/init.d/httpd

Add this code, but change it to meet your specific case.

添加此代码,但对其进行更改以满足您的特定情况。

ORACLE_HOSTNAME=ora11g.home.com; export ORACLE_HOSTNAME
ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1; export ORACLE_HOME
ORACLE_SID=ora11g; export ORACLE_SID
ORACLE_TERM=xterm; export ORACLE_TERM
PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH; export PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib;
export LD_LIBRARY_PATH
CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;
export CLASSPATH

This solved my problem. I hope this helps.

这解决了我的问题。我希望这有帮助。