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

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

问题描述

我喜欢它在 Zend Framework 中的工作方式.通过检查控制器中的 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 代码中检查这个环境变量?

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

或者只是

  • getenv — 获取值环境变量

如果您查看 ZF 项目中的 public/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.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.

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

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