Better Environment Detection for Laravel 4
March 5 2014, 1:37pm
In Laravel's documentation they say you can specify machine names for your app to determine what environment settings to use (determining off url is no longer supported for security reasons). This can be tedious, especially if you have lot of developers using the app, and multiple staging environments. Laravel docs say you can redefine the function as a closure to determine environment however you want.
I wanted it to determine whether it's in local, staging, or production based off a file where the developer can set it in app/config/environment. I modified an idea by Mior Muhammad Zaki on crynobone.com to achieve this.
app/config/environment.php.dist
Create the following file which users will copy to set their environment settings:
<?php
/*
|--------------------------------------------------------------------------
| Application Environment Mode
|--------------------------------------------------------------------------
|
| Set the environment the app should be operating in. The environment name
| will specify which folder of configs to use.
|
| Current options are: local, production.
|
*/
return 'local';
/* End of bootstrap/environment.php */
bootstrap/start.php
Update bootstrap/start.php, move the bindInstallPaths() above the detectEnvironment() call so we can use the app paths inside our detectEnvironment() function, and tell detectEnvironment() to get the environment name from app/config/environment.php
/*
|--------------------------------------------------------------------------
| Bind Paths
|--------------------------------------------------------------------------
|
| Here we are binding the paths configured in paths.php to the app. You
| should not be changing these here. If you need to change these you
| may do so within the paths.php file and they will be bound here.
|
*/
$app->bindInstallPaths(require __DIR__.'/paths.php');
/*
|--------------------------------------------------------------------------
| Detect The Application Environment
|--------------------------------------------------------------------------
|
| Laravel takes a dead simple approach to your application environments
| so you can just specify a machine name for the host that matches a
| given environment, then we will automatically detect it for you.
|
*/
$env = $app->detectEnvironment(function () use ($app) {
return require $app['path'] . '/config/environment.php';
});
.gitignore
Add the following to your .gitignore:
/app/config/environment.php
readme.md
Add to your readme.md that during installation the developer should run:
cp app/config/environment.php.dist app/config/environment.php
- Tags:
- laravel