通过虚拟主机配置设置Application_ENV和PHP阅读 [英] Set Application_ENV via virtual host config and read this in PHP
问题描述
我想这是如何工作的Zend框架。我知道我目前在我的控制器检查APPLICATION_ENV不断使用的环境。
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>
但不幸的是,我不能在我目前的项目中使用ZF。我怎么能在我的PHP code检查这个环境变量?
But unfortunately I can't use ZF in my current project. How can I check this environment variable in my PHP code?
推荐答案
SETENV 设置的值到Apache的环境中,你可以得到它。
Since SetEnv set's the value to Apache's environment, you can get it with
-
apache_getenv
- 找一个Apache subprocess_env变量
或只是
-
GETENV
- 获取环境变量的值
如果你看看一个ZF项目公共/ index.php文件
,你会看到ZF使用 GETENV
:
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'));
这是经常使用的另一种方法是看从PHP的主机名和定义常量相应:
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屋!