在 PHP 脚本(通常是 index.php)中,我使用 -
// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
这将检查可能存储该值的位置的可能层次结构,并在上述三元条件下默认为“生产”。
在本地(运行 Apache)上,它从 ../Apache2/conf/extra/httpd-vhosts.conf 中的 SetEnv APPLICATION_ENV“开发”中获取值
<VirtualHost *:80>
ServerName foo.local
DocumentRoot "C:\wwwroot\foo"
<Directory "C:\wwwroot\foo">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
SetEnv APPLICATION_ENV "development"
ErrorLog "logs/foo.local-error.log"
CustomLog "logs/foo.local-access.log" common
</VirtualHost>
SetEnv APPLICATION_ENV "development" 也可以放在 .htaccess 中,我假设 .htaccess 优先。
当我部署到生产服务器时,常量 APPLICATION_ENV 从同名的 IIS 根服务器名称应用程序设置中获取其值(此服务器上的所有站点都从根继承,但您可以为它们提供自己的本地环境变量)。
可以创建的环境变量Computer->Properties->Advanced System Settings-System Variables似乎优先。IIS 根目录。然后是个别网站。