• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter Command-Line w/ Environment Configs

Does anyone have a good method for using CI in command-line while maintaining dynamic environment detection?

I modify the index.php to switch the environment type based on the HTTP_HOST. Obviously, in command line this value doesn't get set so I can't use that.

I've tried a few other options with little luck. I can pass the environment type in as a parameter, but that's kind of sloppy and requires me to then fill in every parameter for a given method (even optional ones).

Any ideas?

I have no CLI experience with CodeIgniter or PHP, so this is an assumption. But is there some way you can print the contents of $_SERVER to your terminal, to see what variables are available that you can potentially use to set the environment?

Indeed you can, and I have. But I didn't notice anything that stuck out. On my dev box (windows) it shows the computer name. That would have been and option since my prod and uat servers have distictive name patterns. Unfortunately, CentOS does not report the name in the SERVER vars.

Our goal is to send:
php index.php cron daily_tasks important_job 123 --env production

and have it run http://mysite.com/cron/daily_tasks/important_job/123
using the production environment


// it's in two places - let's be smart
define('DEFAULT_ENV', 'development');

// detects if it is a command line request
if ((php_sapi_name() == 'cli') or defined('STDIN'))
$environment = DEFAULT_ENV;
if (isset($argv))
// grab the --env argument, and the one that comes next

$key = (array_search('--env', $argv));
$environment = $argv[$key +1];

// get rid of them so they don't get passed in to our method as parameter values

unset($argv[$key], $argv[$key +1]);
define('ENVIRONMENT', $environment);

if (!defined('ENVIRONMENT')) define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : DEFAULT_ENV);

Here's a quick write up: http://codebyjeff.com/blog/2013/10/setti...ommandline

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.