duanben4771 2012-09-09 11:28
浏览 18
已采纳

在PHP中使用动态配置文件有什么问题?

I have recently taken to converting my websites files into ones that work on any path. I am working on using Virtual Hosts to be able to maintain the same .htaccess file as on the main production server. My initialize file that includes all my models is written dynamically to find out the exact system file path and update the website accordingly so I can upload the whole website anywhere.

The trouble lies in the config file. Depending on whether I am on dev or on prod, I connect to a differert database. Therefore I have a small check first to see if I am running a local server. I do this by storing my naming conventions for my dev in an array and by looping through the array to see if the HTTP_HOST matches a value.

Example:

$dev_names = array('localhost','.dev');

However, when I mentioned this on Chat the other day, a respected user told me that he never does this. Instead he has 2 config files and includes the right one at the right time? I didn't get though what was wrong with my approach?

Can you point out to me the flaws in my plan to help me understand the experts?

(PS. For a bonus you can give me a quick run down on how a bootstrap works, heard that word being thrown around, not sure if it is written in PHP or whatever.)

  • 写回答

3条回答 默认 最新

  • dqnhfbc3738 2012-09-09 11:35
    关注

    I dont see anything wrong with your approach, and I cannot think of a good reason not to do it.

    Personally I do this in my php file;

     if ($_SERVER['SERVER_NAME'] == 'localhost')
     {
        define('ENVIRONMENT', 'development');
     }
     else
     {
        define('ENVIRONMENT', 'production');
     }
    

    And then all my configs hang off that. Works extremely well - means I literally have zero config changes when moving between Dev & production

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改