Laravel - 自定义 .env 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38823145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - custom .env file
提问by SmxCde
Laravel assumes that .env
file should describe environment, and it should not be committed to your repo.
Laravel 假设.env
文件应该描述环境,并且不应该提交到你的仓库。
What if I want to keep both .env
files for dev
and production
(say .env-production
and .env-dev
) in my repo and add some custom logic to which file should be used, for example based on current domain name.
如果我想在我的存储库中保留和(比如和)的两个.env
文件,dev
并向应该使用的文件添加一些自定义逻辑,例如基于当前域名,该怎么办。production
.env-production
.env-dev
Something like
就像是
if ($_SERVER['HTTP_HOST'] == 'prod.domain.com') {
load('.env-production');
} else {
load('.env-dev');
}
How can i achieve it? Thanks!
我怎样才能实现它?谢谢!
采纳答案by SmxCde
Nadeem0035gave me pretty good idea what to do
Nadeem0035让我很清楚该怎么做
bootstrap\app.php
right before return $app;
bootstrap\app.php
就在之前 return $app;
$envFile = $_SERVER['HTTP_HOST'] == 'prod.domain.com' ? '.env-production' : '.env-dev';
$app->loadEnvironmentFrom($envFile);
回答by Nadeem0035
Use Dotenv::load() for custom .env file
使用 Dotenv::load() 自定义 .env 文件
laravel 5.1 with vlucas/phpdotenv ~1.0
laravel 5.1 与 vlucas/phpdotenv ~1.0
if ($_SERVER['HTTP_HOST'] == 'prod.domain.com') {
Dotenv::load(__DIR__ . '/../','.production.env');
} else {
Dotenv::load(__DIR__ . '/../','.dev.env');
}
OR
或者
laravel 5.2 with vlucas/phpdotenv ~2.0
laravel 5.2 与 vlucas/phpdotenv ~2.0
$dotenv = new Dotenv\Dotenv(__DIR__, 'myconfig'); // Laravel 5.2
$dotenv->load();
In bootstrap/app.php
在 bootstrap/app.php
回答by Skid Kadda
I like to add a solution for people who have a shared codebase for many vhoststhat all need different .env
files for all the different things, like database connections, smtp settings etc.
我喜欢为那些拥有许多虚拟主机共享代码库的人添加一个解决方案,这些虚拟主机都需要不同的.env
文件来处理所有不同的事情,例如数据库连接、smtp 设置等。
For every vhost, on Apache, create a vhost config:
对于每个vhost,在 Apache 上,创建一个 vhost 配置:
<VirtualHost *:80>
ServerName your-vhost.yourdomain.com
DocumentRoot /var/www/shared-codebase/public
SetEnv VHOST_NAME 'your-vhost'
<Directory "/var/www/shared-codebase/public">
Options Indexes MultiViews FollowSymLinks
AllowOverride all
Order deny,allow
Require all granted
</Directory>
<IfModule mpm_itk_module>
AssignUserId your-vhost your-vhost
</IfModule>
ErrorLog /var/www/your-vhost/logs/error.log
CustomLog /var/www/your-vhost/logs/access.log combined
</VirtualHost>
All vhosts have the same document root and directory, because it is a shared codebase. Inside the config we added a SetEnv VHOST_NAME 'your-vhost'
which we will later use in Laravel's bootstrap.php to change the location of vhost specific .env
.
所有虚拟主机都具有相同的文档根和目录,因为它是一个共享代码库。在配置中,我们添加了一个SetEnv VHOST_NAME 'your-vhost'
我们稍后将在 Laravel 的 bootstrap.php 中使用它来更改 vhost specific 的位置.env
。
Next create the custom .env file
in a folder(fe. /var/www/your-vhost/.env) the alter bootstrap.php
so that it loads the .env from the right location.
接下来.env file
在文件夹中创建自定义(fe. /var/www/your-vhost/.env)alterbootstrap.php
以便它从正确的位置加载 .env。
<?php
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Add the location of the custom env file
|--------------------------------------------------------------------------
*/
$app->useEnvironmentPath('/var/www/'.$_SERVER['VHOST_NAME']);
return $app;
That's all.
就这样。
[edit]I you want to target a specific database or want to generate a key for a specific .env, then you should put the VHOST_NAME in front of the artisan command.
[编辑]如果您想针对特定数据库或想要为特定 .env 生成密钥,那么您应该将 VHOST_NAME 放在 artisan 命令的前面。
VHOST_NAME=tenant2.domain.com php artisan key:generate
[edit]When working locally and are using Laravel Valet, then you can add a custom .valet-env.php
in the root of your codebase.
https://laravel.com/docs/master/valet#site-specific-environment-variables
[编辑]在本地工作并使用 Laravel Valet 时,您可以.valet-env.php
在代码库的根目录中添加自定义。
https://laravel.com/docs/master/valet#site-specific-environment-variables
回答by Dhaval Tailored
You've single .env file into laravel and you can define level of your app.
APP_ENV=local
OR
APP_ENV=production
You can set configuration as per your requirement and not need to create new .env file to here. More about Laravel Environment Variables:
and Here's more descriptive help for you: phpdotenv
您已将 .env 文件放入 Laravel,并且可以定义应用程序的级别。
APP_ENV=local
或者
APP_ENV=production
您可以根据您的要求设置配置,而无需在此处创建新的 .env 文件。有关Laravel 环境变量的更多信息:这里为您提供更多描述性帮助:phpdotenv
回答by Sir_Faenor
I'd like to share my two cents on this, for team working on different machines / hosts.
I create a directory env
on app's root, containing:
对于在不同机器/主机上工作的团队,我想分享我的两分钱。我env
在应用程序的根目录上创建了一个目录,其中包含:
- a
.master.env
file with the main and shared configuration. - a
.name
file containing only a string with the environment's name for specific machine / purpose (e.g. "server1") - the specific .env file matching the name defined above, e.g.
.server1.env
.
- 一个
.master.env
包含主配置和共享配置的文件。 .name
只包含一个字符串的文件,其中包含特定机器/用途的环境名称(例如“server1”)- 与上面定义的名称匹配的特定 .env 文件,例如
.server1.env
.
Then, in bootstrap/app.php:
然后,在 bootstrap/app.php 中:
/**
* master config
*/
$app->useEnvironmentPath(__DIR__.'/../env');
$app->loadEnvironmentFrom('.master.env');
/**
* config overloading
*/
$app->afterLoadingEnvironment(function() use($app) {
$envFile = trim(file_get_contents($app->environmentPath().'/.name'));
if ($envFile && file_exists($app->environmentPath().'/.' .$envFile .'.env')) {
$dotenv = Dotenv\Dotenv::create($app->environmentPath(), '.'.$envFile.'.env');
$dotenv->overload();
}
});
Now you can selectively override configuration keys for specific machines and if you don't have security issues you can put the env files in VCS , as long as you ignore the `.name' file.
现在您可以有选择地覆盖特定机器的配置键,如果您没有安全问题,您可以将 env 文件放在 VCS 中,只要您忽略 `.name' 文件。
Working in Laravel 5.8.
在 Laravel 5.8 中工作。