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

Laravel 4 and NodeJs/Redis pub/sub realtime notifications

Currently I am building an application where we can fill in live scores and I needed something to update all my visitors whenever a score has been updated by one of the admins.

Whenever an admin updates the score via the Laravel 4 backend I fire an event and publish it to Redis. I’ve setup a simple NodeJS server which listens to Redis for incoming changes. NodeJS will redirect the message to all Socket.IO clients.

Continue reading Laravel 4 and NodeJs/Redis pub/sub realtime notifications

Laravel and HTML 5 datetime component

I want to store all dates in UTC format into the database. To use HTML5 datetime-local element you have to do some conversion of datetimes. The HTML5 element expects a format of ‘2013-08-14T18:55’ (Y-m-d*H:i). In this post I’ll show you how I solved this.

Continue reading Laravel and HTML 5 datetime component

Laravel 4: Redirect to the intended url after login

The best way to redirect a user who is not logged in is to use the ‘Redirect::guest(..)’ method. This method will store the intended url in the session of the user. After logging in he can be be redirected back to the intended url via ‘Redirect:intended(..)’.

See example below.
Continue reading Laravel 4: Redirect to the intended url after login