Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment detection in Laravel 4.1+

Tags:

php

laravel

Laravel 4.1 removed the feature to use the domain for detecting what environment the app is running in. Reading the docs they now suggest using host names. However, to me that seems cumbersome if you are working in a team. Should everyone change the bootstrap/start.php file and add their own host name to be able to run the app in a dev environment? Also, what if you want to have two different environments on the same machine?

How to best detect the environment if you are working in a team in Laravel 4.1+?

like image 829
Simon Bengtsson Avatar asked Feb 12 '14 14:02

Simon Bengtsson


2 Answers

Here is my settings from bootstrap/start.php file:

$env = $app->detectEnvironment(function() use($app) {
    return getenv('ENV') ?: ($app->runningInConsole() ? 'local' : 'production');
});

Instead of default array, this method in my case is returning closure with ternary. That way I got more flexibility in choosing desired environment. You can use switch statement also. Laravel will read return value and configure itself. With getenv native function, I am just listening for a given environment. If my app is on the server, then it will "pick" server configurations. If locally, then it will pick local (or development) And don't forget to create custom directories for you environemnts in app/config There is also testing env, which is choosen automatically, every time you are unit testing app.

Laravel makes working with environments really fun.

UPDATE:

With environments we are mostly cencerned about db credentials.

For production I use Fortrabbit, so when configuring new app on server, fortrabbit is generating those values for me. I just need to declare them. e.g. DB of just database or db... Or DB_HOST or HOST ... Locally, those values are the one you use for your localhost/mysql settings.

like image 65
Miroslav Trninic Avatar answered Oct 17 '22 01:10

Miroslav Trninic


Update:

In Laravel 5.0 environment detection is no longer needed in the same way. In the .env file you can simply have a variable for which environment the app should run in.

Old answer for Laravel <= 4.2

What I ended up doing is very close to what carousel suggested. Anyway, thought I would share it. Here is the relevant part of our bootstrap/start.php file:

$env = $app->detectEnvironment(function ()
{
    if($app->runningInConsole())
        return "development";

    $validEnvironments = array("development", "staging", "production");
    if (in_array(getenv('APP_ENV'), $validEnvironments)) {
        return getenv('APP_ENV');
    }
    throw new Exception("Environment variable not set or is not valid. See developer manual for further information.");
});

This way all team members have to declare an environment variable somewhere. I haven't really decided if throwing an exception if the environment variable is not set or just default to production is the best thing. However, with the above, it's easy to change.

like image 3
Simon Bengtsson Avatar answered Oct 16 '22 23:10

Simon Bengtsson