设置测试和生产环境
我正在 CakePHP 中开发一个系统,使用 Git 作为版本控制系统。 我的测试服务器中有一个工作副本,生产服务器中有另一个工作副本,两者都具有不同的数据库。 每次进行更改时,我都必须更改数据库配置,以便测试系统。 是否有另一种方法来保留两个具有不同内容的文件,一个在测试中,另一个在生产服务器中? 分支机构是一个好方法吗?
I'm developing a system in CakePHP, using Git as the version control system. I have a working copy in my testing server and another in my production server, both with different databases. Everytime I make changes, I have to change the database configuration so I can test the system. Is there another way to keep two files, with different contents, one in the test and another in the production server? Branches are a good way to go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不会想用分支来做到这一点。 我不能具体谈论 PHP,但不是将两个文件(测试配置和生产配置)保存在单独的分支中,而是将它们放在一起,但只是让环境变量确定哪个配置是正确的使用在运行时。
(这就是 Rails 中的实现方式,而且效果很好。)
You wouldn't want to do that with branches. I can't speak to PHP specifically, but rather than keep the two files (test config & production config) in separate branches, you'd keep them together, but just let an environment variable determine which config is the correct one to use at runtime.
(This is how it's done in Rails and it works well.)
我采用了一种相当粗糙但有效的技术:在我的开发环境中,我有一个名为“environment_development”的空文件。 在我的生产环境中,我有一个名为“environment_Production”的环境(不同情况以增加视觉强调)。 我的 gitignore 文件设置为忽略这两者。
我的应用程序的前端控制器(我使用 Kohana 框架,但我假设 CakePHP 有类似的东西)检查这些文件是否存在并适当地设置 IN_Production 常量。 代码的其余部分(数据库配置、错误处理等)可以检查该常量的值并根据需要更改行为。
我曾经使用 $_SERVER['SERVER_NAME'] 检查,但此方法具有以下优点:
I've gone for a rather crude but effective technique: In my development environment I have an empty file called 'environment_development'. In my production environment I have one called 'environment_PRODUCTION' (different case for added visual emphasis). My gitignore file is set to ignore both of these.
The front controller of my application (I use the Kohana framework, but I'm presuming CakePHP has something similar) checks for the presence of these files and sets an IN_PRODUCTION constant appropriately. The rest of the code (database configuration, error handling etc.) can check the value of this constant and alter behaviour as required.
I used to use the $_SERVER['SERVER_NAME'] check, but this method has the following advantages:
如果数据库依赖环境,你可以在database.php文件中做这样的事情:
If the database is dependent on the environment, you can do something like this in the
database.php
file: