问题
I understand with Laravel 5, it uses .env files so we can set specific enivornment values.
My question is, is there a way in Laravel 5, to say for example,
if ($SERVER_NAME == "my_production_server") {
$environment = "production"
}
And from that it uses production values. My thinking is, I'd like all the environments and their variables placed in one file or directory, or whatever so we can deploy the whole build without any manual intervention, and we can check this all into our code repository.
回答1:
Laravel 5 has made this a little harder than before but here's the way to do it.
All you will need to do after this is change a value of your .env
file and the environment will change
The steps to do this are as follows
Look at your local
.env
installed by Laravel and change its contents to local or production or whatever else you needCreate 2 files
.local.env
and.production.env
Add default environment value:
- In
.local.env
:APP_ENV=local
- In
.production.env
:APP_ENV=production
- In
Create new php file and named it,
environment.php
, save it into this folder:app/bootstrap/environment.php
$env = $app->detectEnvironment(function(){ $environmentPath = __DIR__.'/../.env'; $setEnv = trim(file_get_contents($environmentPath)); if (file_exists($environmentPath)){ putenv("APP_ENV=$setEnv"); if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) { Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env'); } } });
Include your
environment.php
file in bootstrap file. Paste it inside yourbootstrap/app.php
file.require __DIR__.'/environment.php';
Yay! You're done.
NOTE: If Laravel can't find a .env
file it automatically uses .production.env
which makes it awesome for deployments
Credit to http://developers.ph/laravel-framework/laravel-5/how-to-setup-multiple-environment-for-laravel-5-developers-way/
回答2:
You could probably set the .env as:
APP_ENV=local
APP_DEBUG=true
APP_KEY=sjkanljksdnjsnetcetcetcetc..
APP_URL=http://localhost:8000
DB_HOST=qa
DB_DATABASE=admin
DB_USERNAME=home
DB_PASSWORD=root
DB_HOST2=production// I.P address
DB_DATABASE2=admin
DB_USERNAME2=admin
DB_PASSWORD2=admin
Then in the database.php file in the config folder, you could set up the connections Host and Host2 to match the localhost and production values.
回答3:
I have it like this For Laravel 5.0. I followed cjds guide but changed the code my specification. It requires no .env files.
$env = $app->detectEnvironment(function() {
if (php_sapi_name() === 'cli') {
if (strpos(getcwd(), 'production') !== false) {
return 'production';
} elseif (strpos(getcwd(), 'staging') !== false) {
return 'staging';
} elseif ((strpos(getcwd(), 'xampp') !== false) || (strpos(getcwd(), 'lampp') !== false)) {
return 'development';
} else {
return 'production';
}
}
$absoluteLink = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (strpos($absoluteLink, 'production') !== false) {
return 'production';
} elseif (strpos($absoluteLink, 'staging') !== false) {
return 'staging';
} elseif (strpos($absoluteLink, '.dev') !== false || strpos($absoluteLink, 'local') !== false || strpos($absoluteLink, '192.168') !== false) {
return 'development';
} else {
return 'production';
}
});
putenv("APP_ENV=" . $env);
来源:https://stackoverflow.com/questions/31082173/laravel-5-environment-detection-by-server-name