Laravel 5 Dotenv for specific subdomain
Asked Answered
B

4

5

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 ?

Barmy answered 26/4, 2015 at 16:32 Comment(0)
U
5

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.

Unaccountable answered 26/4, 2015 at 18:2 Comment(2)
@Artistan Probably you are using newer version of framework and you need to load it in different way (this is post from over 1 year).Hungarian
Many Thanks, it was very helpful to build my solution, here, this works for laravel 5.2 :) GreetingsWilke
D
4

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! :) )

Drape answered 11/12, 2015 at 15:21 Comment(0)
H
0

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.

Haskell answered 26/4, 2015 at 16:41 Comment(0)
W
0

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

Wilke answered 28/10, 2016 at 17:27 Comment(1)
Just notice that HTTP_HOST might not be always set. For example, when you run artisan command it won't be setHungarian

© 2022 - 2024 — McMap. All rights reserved.