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

#1
[eluser]jmadsen[/eluser]
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:

Code:
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

Jeff

#2
[eluser]jmadsen[/eluser]
So, decided to toss together a little explanatory tut on the above code:

http://codebyjeff.com/blog/2013/10/setti...ommandline

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.