PHP: One way of differing between DEV and PROD environments with Kohana

Usually when I develop websites, they will be deployed to a domain, for example www.geekality.net. But when I develop this site locally, I usually want it to be in some sub-folder of localhost, since I usually have more than one website going on, and I don’t want the trouble with virtual-hosts. So, to solve this for my Kohana websites I do the following simple thing. This technique would work for other websites programmed in PHP as well, but the PHP part would probably be a bit different.

Apache httpd.conf

First of all, I add this to the end of my httpd.conf.

# Custom htaccess file
AccessFileName .htaccess.local .htaccess

Make sure to check if there is already a line with AccessFileName though, and if it is, just edit that instead of adding another one :)

Environment variables

With that done, you can have one regular .htaccess which would be used in production and a local one for your machine.

#.htaccess
SetEnv KOHANA_ENV production
SetEnv KOHANA_BASE /
RewriteBase /
#.htaccess.local
SetEnv KOHANA_ENV development
SetEnv KOHANA_BASE /geekality/
RewriteBase /geekality/

These two files can now live side by side, checked in to source control, and not cause any trouble. Alternatively you could set the local one to be ignored so that other developers could create their own setup.

Kohana bootstrap

Now comes the Kohana specific part. In my application/bootstrap.php I have the following (among other stuff of course).

if (isset($_SERVER['KOHANA_ENV']))
{
  Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}

Kohana::init(array(
  'base_url'   => $_SERVER['KOHANA_BASE'],
  'index_file' => '',
  'profile' => Kohana::$environment !== Kohana::PRODUCTION,
  'caching' => Kohana::$environment !== Kohana::DEVELOPMENT,
  'errors' => TRUE,
));

And that’s pretty much all there is to it. This way I don’t have to worry about not overwriting the .htaccess on my production server or stuff like that. The .htaccess is untouched and just for the production environment :)

One thought on “PHP: One way of differing between DEV and PROD environments with Kohana

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>