开发者

How to configure CodeIgniter to automatically detect development machine and real machine?

I'm developing from my Windows laptop but need to test my development on my shared Linux hosting. I have thrown together the following in place of the normal $application_path = "application"; line.

$env['laptop']['ip'] = '127.0.0.1';
$env['laptop']['host'] = 'MATT-WINDOWS7';
$env['laptop']['path'] = 'private';

$env['mattpotts']['ip'] = '12.34.56.78'; $env['mattpotts']['host'] = 'my.webhost.com'; $env['mattpotts']['path'] = '../private/blah';

 $ip = $_SERVER['SERVER_ADDR'];
 $host = php_uname('n');
 foreach($env as $e)
  if($e['ip'] == $ip && $e['host'] == $host)
   $application_folder = $e['path'];
 unset($env);
 if(!isset($application_folder))
  die('application folder not set');

...which works fine for setting the application path but now I'm running into trouble with the need for a database config for each environment.

I can m开发者_JS百科ake it work with a few simple ifs but I was wondering if there's a best practice solution to this one.

Cheers


Use revision control such as Subversion. Have one configuration file deployed to your test environment and a modified version checked out in your development environment. Simply tell your client to not commit those configuration changes so they don't make it to your testing/production environment.

This is definitely the best practice solution :)

P.S. If you don't feel like setting up a Subversion server, there's always hosted solutions like Beanstalk and if you're on Windows, TortoiseSVN is a slick client.


If you don't feel like setting up subversion you can always detect what site you're on by looking at SERVER_NAME. In a CI site in the past I used the following within my config.php to figure out dev vs production servers:

if ($_SERVER['SERVER_NAME'] == 'www.mysite.com') {
    $config['log_path'] = '/var/log/site/';
} else {
    $config['log_path'] = '/var/log/dev_site/';
}

You can use this anywhere you need to have different variables based on environment. That being said hard-coding stuff like this into your code isn't always the best idea.


Here's a nice clean solution to running multiple production environments on a single codebase:

http://philsturgeon.co.uk/news/2009/01/How-to-Support-multiple-production-environments-in-CodeIgniter


As Phil's solution is no longer accessible, here's another solution that'll provide you with the exact same solution.

Here's the link to the Git repo: https://github.com/jedkirby/ci-multi-environments and this is a brief explanation of the module: http://jedkirby.com/blog/2012/11/codeigniter-multiple-development-environments


Phil Sturgeons page has moved to here: http://philsturgeon.co.uk/blog/2009/01/How-to-Support-multiple-production-environments-in-CodeIgniter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜