Laravel 5 Dotenv for specific subdomain

2019-04-12 02:34发布

I have a few subdomain in my laravel 5 application, each sub domain have a specific configuration, like mail, nocaptcha, etc.

how to set .env file to work with my-specific subdomain ?

4条回答
Fickle 薄情
2楼-- · 2019-04-12 03:09

I had the same issue, Based on @Marcin's answer I built this one (Works with laravel 5.2.X)

I added in the bootstrap/app.php

if (isset($_SERVER['HTTP_HOST'])) {
    $hostArray = explode('.', $_SERVER['HTTP_HOST']);
    //if the address is a subdomain and exist the .xxx.env file
    $envFile = sprintf('.%s.env', $hostArray[0]);
    if (count($hostArray) >  2 && file_exists(sprintf('%s/%s', $app['path.base'], $envFile))) {
        $app->loadEnvironmentFrom($envFile);
    }
}

after

$app = new Illuminate\Foundation\Application( realpath(__DIR__.'/../') );

I hope that helps someone

Greetings

查看更多
冷血范
3楼-- · 2019-04-12 03:14

The best solution I found is to use .htaccess plus env variables.

In .htaccess add these lines:

<If "%{HTTP_HOST} == 'sub.domain'">
    SetEnv APP_DOMAIN sub
</If>

In bootstrap/app.php add after the app initialisation:

//own env directory for separate env files
$app->useEnvironmentPath( realpath(__DIR__ . '/../env/') );
//separate files for each domain (see htaccess)
$app->loadEnvironmentFrom( getenv('APP_DOMAIN') . '.env' );

Create a new directory called "env" in your Laravel root and add your config files as:

  • "sub1.env",
  • "sub2.env" ..etc

(Of course you can keep it in your root as is currently, but for many subdomains it's better to move into a directory => looks much cleaner => everyone's happy! :) )

查看更多
Anthone
4楼-- · 2019-04-12 03:29

You can’t. Each subdomain will be running in the same environment.

If you want per-subdomain configuration then your best bet is to either create a configuration file in the config directory with each subdomain’s settings, or use a database approach.

查看更多
迷人小祖宗
5楼-- · 2019-04-12 03:35

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.

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);
});

Now modify bootstrap/app.php to load your custom env.php file. Just add:

require('env.php');

after

$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

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.

查看更多
登录 后发表回答