Laravel 5 Dotenv用于特定子域 [英] Laravel 5 Dotenv for specific subdomain
问题描述
我的laravel 5应用程序中有几个子域,每个子域都有特定的配置,例如mail,nocaptcha等.
I have a few subdomain in my laravel 5 application, each sub domain have a specific configuration, like mail, nocaptcha, etc.
如何设置.env文件以与我特定的子域一起使用?
how to set .env file to work with my-specific subdomain ?
推荐答案
是的,您可以为每个子域使用单独的.env
文件,因此,如果在配置中使用env变量,则无需进行大量修改即可工作.
Yes, you can use separate .env
files for each subdomain so if you use env variables in your config it will work without great modifications.
创建具有以下内容的bootstrap/env.php
文件:
Create bootstrap/env.php
file with the following content:
<?php
$app->detectEnvironment(function () use ($app) {
if (!isset($_SERVER['HTTP_HOST'])) {
Dotenv::load($app['path.base'], $app->environmentFile());
}
$pos = mb_strpos($_SERVER['HTTP_HOST'], '.');
$prefix = '';
if ($pos) {
$prefix = mb_substr($_SERVER['HTTP_HOST'], 0, $pos);
}
$file = '.' . $prefix . '.env';
if (!file_exists($app['path.base'] . '/' . $file)) {
$file = '.env';
}
Dotenv::load($app['path.base'], $file);
});
现在修改bootstrap/app.php
以加载您的自定义env.php
文件.只需添加:
Now modify bootstrap/app.php
to load your custom env.php
file. Just add:
require('env.php');
之后
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
现在您可以为每个域创建单独的环境文件,例如,如果使用testing.app
,abc.testing.app
和def.testing.app
,则可以为主域(以及所有未自定义的子域)提供.env
文件环境文件)以及用于自定义环境变量的.abc.env
和.def.env
文件作为您的子域.
Now you can create separate env files for each domains for example if you use testing.app
, abc.testing.app
and def.testing.app
you can have .env
file for main domain (and for all subdomains that don't have custom env files) and .abc.env
and .def.env
files for custom env variables your your subdomains.
这篇关于Laravel 5 Dotenv用于特定子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!