通过虚拟主机配置设置Application_ENV并在PHP中读取 [英] Set Application_ENV via virtual host config and read this in PHP

查看:918
本文介绍了通过虚拟主机配置设置Application_ENV并在PHP中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢这在Zend Framework中如何工作。我可以通过检查我的控制器中的APPLICATION_ENV常数知道我目前使用的环境。

 < VirtualHost *:80> 
#ServerName
#DocumentRoot

SetEnv APPLICATION_ENVdevelopment

#Directory
< / VirtualHost>

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

解决方案

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





或只是








如果你看一个ZF项目中的 public / index.php ,你会看到ZF使用 getenv

  //定义应用程序环境
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',(getenv('APPLICATION_ENV')?
getenv('APPLICATION_ENV'):
'production'));






常用的替代方法是读取主机名从PHP中定义常量:

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

这样,你不必依赖环境设置。


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.

<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?

解决方案

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

or just

  • getenv — Gets the value of an environment variable

If you look at public/index.php in a ZF project, you will see ZF uses 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:

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

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

这篇关于通过虚拟主机配置设置Application_ENV并在PHP中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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