关于定义环境的方法

WBOY
Release: 2016-06-06 20:10:46
Original
910 people have browsed it

不知道,大家都是怎么样定义自己的项目的运行环境的,我说的环境是线上(master),测试(beta),开发(dev),本地(local)这种,像我们是通过设置机器的host来做到的

<code>// 环境选择
if (gethostname() === 'TBJ-Test') {
    define('ENV_MODE', 'dev');
} elseif (gethostname() === 'TBJBeta') {
    define('ENV_MODE', 'beta');
}  elseif (strpos(gethostname(), 'TBJ-') === 0) {
    define('ENV_MODE', 'local');
} else {
    define('ENV_MODE', 'online');
}</code>
Copy after login
Copy after login

不知道大家都有什么比这个更好的方法?

回复内容:

不知道,大家都是怎么样定义自己的项目的运行环境的,我说的环境是线上(master),测试(beta),开发(dev),本地(local)这种,像我们是通过设置机器的host来做到的

<code>// 环境选择
if (gethostname() === 'TBJ-Test') {
    define('ENV_MODE', 'dev');
} elseif (gethostname() === 'TBJBeta') {
    define('ENV_MODE', 'beta');
}  elseif (strpos(gethostname(), 'TBJ-') === 0) {
    define('ENV_MODE', 'local');
} else {
    define('ENV_MODE', 'online');
}</code>
Copy after login
Copy after login

不知道大家都有什么比这个更好的方法?

我这有两种方案:
一是在配置文件里写。不过这种方法不太好用,特别是项目有版本库已经需要持续集成的时候,会比较麻烦。
二是通过特定的文件来识别,比如上线版里有个release.lock的空文件,程序检测到这个文件就认为是上线版,在入版本库的时候可以将这几个识别文件忽略掉。

差不多,是在服务器上,自定义环境变量
比如 laravel,在 nginx 的配置文件里

<code>fastcgi_param  APP_ENV            testing;</code>
Copy after login

然后. env文件的后缀根据环境变量加载,会加载.env.testing配置文件
所以现在也比较习惯用这种形式

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!