Laravel 4 and environment detection

Lately I was struggling with the environment detection in Laravel 4.1. I am using one VPS for my staging and production environment. So environment detection based on the hostname won’t work for my situation because they are the same. URL detection has been removed in 4.1 due to security issues.

I found a blog post which almost suits my needs. The problem with this approach was that every time I do a deployment (via Rocketeer) I had to create a new environment.php to specify which environment it should use.

Rocketeer pulls a Github repository into a releases folders and creates a symlink from the ‘current’ directory to the right release. Rocketeer has also a ‘shared’ section which will remain (logs / sessions / settings (PhilF)) on deployment. So that would be a nice location for the environment.php

app/storage/settings/environment.php

<?php
return 'staging'; //or 'production'

So what you should do is to change the detectEnvironment (bootstrap/start.php) file like this:

$app->bindInstallPaths(require __DIR__.'/paths.php');

$env = $app->detectEnvironment(function () use ($app) {

    $environment = include $app['path.base'].'/app/storage/settings/environment.php';
    return $environment ? $environment : 'local';

});

Note 1: Make sure bindInstallPaths is called before detectEnvironment.

Note 2: I am using ‘include’ instead of ‘required’, because the environment.php is optional. Required will stop the application. When the file is not found it will default to the ‘local’ configuration (for development).

Additional info:
The app/storage/settings directory is a symlink to /var/www/mysite/staging/shared/app/storage/settings

Directory structure:
/var/www/mysite/staging/current –> symlink to a release
/var/www/mysite/staging/releases/201401172200
/var/www/mysite/staging/releases/201401172200/app/storage/settings –> symlink to staging/shared/settings
/var/www/mysite/staging/shared

See also https://gist.github.com/stefandroog/8489315

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>