• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Commandline w/ environments hack

CI's idea of working with the Apache env variables is nice in some ways, but has the issue of not working from the command line

CIs cli syntax is also a little weird to work with, as all extra arguments get passed as variables to your function.

So I threw together this rather crude but I think effective hack:

if ((php_sapi_name() == 'cli') or defined('STDIN'))
$environment = 'localhost';
if (isset($argv))
  $key = (array_search('--env', $argv));
  $environment = $argv[$key +1];
  unset($argv[$key], $argv[$key +1]);
   define('ENVIRONMENT', $environment);

(This of course goes in your index.php, where you define your ENVIRONMENT )

You run it normally, but then can add "--env production" at the end to set your ENVIRONMENT. Yes, simple & crude :-)

Wonder if anyone has solved this in a different manner, or sees any issues with this code (not deployed at all yet, so use at own risk for now). I had considered getting fancy and rewriting the whole initialization bit with a library & pull in from vendor, but just seemed like overkill when CI is so restricted in what it accepts


So, decided to toss together a little explanatory tut on the above code:


Gives you a tiny dip into the Core classes, so good read if you're still relatively new to CI

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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