Laravel 5 Dotenv for specific subdomain

别等时光非礼了梦想. 提交于 2019-12-06 04:46:45

问题


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 ?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/29880331/laravel-5-dotenv-for-specific-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!