Codeigniter配置数据库文件

时间:2013-06-11 08:51:11

标签: codeigniter

我想知道给定codeigniter $_SERVER['SERVER_NAME'],我可以为每种$_SERVER['SERVER_NAME']加载不同的配置文件吗?

例如,如果网址是localhost,请改为运行config_localhost,如果网址处于活动状态,请运行config_live。同样对于database.php也是如此。

到目前为止,我要检查相应的文件和交换机循环以检查服务器名称。

     switch ($_SERVER['SERVER_NAME']) {      
          case KH_SERVER_NAME:  // development server   
              $config['base_url']    = 'localhost';     
              break;

          default:  // live server 
            $config['base_url']    ="http://www.domain.com";    
             break;
        }

1 个答案:

答案 0 :(得分:2)

是的,你可以做这样的事情。我建议把这样的东西放到你的根index.php中,以便它在开始时设置:

  switch( $_SERVER['HTTP_HOST'] )
  {
    case 'www.domainname.com':
      define('ENVIRONMENT', 'production');
    break;
    case 'dev.domainname.com':
      define('ENVIRONMENT', 'development');
    break;
    case 'test.localhost':
    case 'www.test.localhost':
      define('ENVIRONMENT', 'local');
    break;
  }

这定义了一个名为ENVIRONMENT的变量,然后您可以在整个配置文件(和应用程序,如果需要)中使用 - 这样,您可以执行以下操作,而不是测试$ _SERVER变量:

switch (ENVIRONMENT)
{
  case 'local':
  case 'development':
    $config['base_url'] = 'localhost';
  break;

  case 'production':
    $config['base_url']    ="http://www.domain.com";
  break;
}

和其他方便的事情:

switch (ENVIRONMENT)
{
  case 'local':
    error_reporting(E_ALL);
  break;

  case 'development':
    error_reporting(E_ALL & ~E_DEPRECATED);
  break;

  case 'production':
    error_reporting(0);
  break;
}

这也可以在主应用程序中使用,例如,决定是否要在视图中输出分析跟踪代码 - 您通常只想在生产中输出 - 例如:

  <? if(ENVIRONMENT == 'production'): ?>
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXX-Y']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>
  <? endif ?>
相关问题