Simon Bengtsson
Simon Bengtsson

Reputation: 8151

Environment detection in Laravel 4.1+

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+?

Upvotes: 6

Views: 11797

Answers (8)

Wallace Vizerra
Wallace Vizerra

Reputation: 3542

In bootstrap/start.php define this:

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

    $enviromentsHosts = [
        'localhost',
        'local',
    ];

    if ($app->runningInConsole() || in_array($app['request']->getHost(), $enviromentsHosts)) {

        return 'local';

    } else {

        return 'production';
    }
});

I believe it is better to use only the resources of Laravel 4

Upvotes: 0

Simon Bengtsson
Simon Bengtsson

Reputation: 8151

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.

Upvotes: 3

Victor BV
Victor BV

Reputation: 1101

Laravel 4.1 and 4.2 detects the environments through the machine names specified in the "bootstrap/start.php" file.

For example, in my case the config becomes:

$env = $app->detectEnvironment(array(
  'local' => array('Victor.local', 'Victor-PC'),
));

This means that Laravel will use the 'local' environment settings for both machines: 'Victor.local' (a Mac) and 'Victor-PC' (Windows).

This way you can regsiter several machines to work as local environment. Other environments can be registered too.

In order to know the current machine name, you can use the following PHP code:

<?php echo gethostname(); ?>

Hope it helps!

Upvotes: 1

shazbot
shazbot

Reputation: 585

I didn't like that production was default, so I made it anything other than live server will go to local configs:

in bootstrap/start.php :

$env = $app->detectEnvironment(function(){
    if (gethostname() !== 'live_server_hostname'){
        return 'local';
    } else {
        return 'production';
    }
});

Upvotes: 0

Wasim A.
Wasim A.

Reputation: 9890

what i did is, make dir app/config/local and use code

$env = $app->detectEnvironment(function(){
    return $_SERVER['HTTP_HOST']=="localhost"?"local":"production";
});

For localhost and online.

Upvotes: 0

Tấn T&#226;m
Tấn T&#226;m

Reputation: 130

You can use something like this:

$env = $app->detectEnvironment(function(){
    if($_SERVER['HTTP_HOST'] == 'youdomain_local')
    {
        return 'local';
    }elseif($_SERVER['HTTP_HOST'] == 'youdomain_team')
    {
        return 'team';
    }else{
        return 'production';
    }
});

Upvotes: 0

Miroslav Trninic
Miroslav Trninic

Reputation: 3455

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.

Upvotes: 14

alexrussell
alexrussell

Reputation: 14202

For me, I just use 'dev' => '*.local' and it works. I haven't 100% tested in a team situation but I think it'd work (big assumption alert:) assuming you're on OSX and get the default Alexs-iMac.local-like hostname.

As for faking an environment, I'm not sure it's really supported. It'll be doable, but in general the whole point of environments is that dev has entirely different needs to production and the two are mutually exclusive. Having the ability to switch on one physical environment seems counter to that goal.

Upvotes: 1

Related Questions