如何在单个laravel应用中为多个域加载不同的.env文件? [英] how to load different .env file for multiple domain in single laravel app?

查看:107
本文介绍了如何在单个laravel应用中为多个域加载不同的.env文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过多个域访问单个laravel应用程序.

I would like to access single laravel application by multiple domain.

对于路由,我使用了laravel路由模式作为示例.

For routing i have used laravel route pattern for eg.

$appRoutes = function() {
     Route::group(['namespace'=>"Interface1"], function(){

     /** route for index page and User authentication pages **/
     Route::get('/', 'LoginController@showLoginForm')->name('login'); 
 });
};

Route::group(array('domain' => 'example1.com'), $appRoutes);
Route::group(array('domain' => 'example2.com'), $appRoutes);

现在,每个域的.env我已经替换了AppServiceProvider.php注册方法中的config变量值:

Now .env for each domain i have replaced config variable value inside AppServiceProvider.php register method:

   if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST']) ){ 
        if($_SERVER['HTTP_HOST']=='example1.com'){  
            $configArray = [
                'APP_NAME' => 'Application 1',
                'APP_URL'  =>  'http://example1.com', 
            ]; 
        }else{  
            $configArray = [
                'APP_NAME' => 'Application 2', 
                'APP_URL' =>  'http://example2.com, 
            ];  
        }  
        config($configArray);
    } 

但是我仍然无法使用url(config('app.url'))

如何为每个域加载所有.env变量覆盖?

How to load all .env variable overwrite for each domain?

推荐答案

config()设置配置设置,但不设置环境设置.

config() sets the configuration settings but not the environment settings.

您必须执行以下操作:

if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST']) ){ 
    if($_SERVER['HTTP_HOST']=='example1.com'){  
         $configArray = [
            'app.name' => 'Application 2', 
            'app.url' =>  'http://example2.com', 
        ];  ;
    }else{  
        $configArray = [
            'app.name' => 'Application 2', 
            'app.url' =>  'http://example2.com', 
        ];  
    }  
    config($configArray);
} 

然后您可以通过以下方式检索值:

You can then retrieve the values via:

config("app.name");
config("app.url");

据我所知,在实际加载配置之前无法修改环境变量,因为配置已加载并在注册服务提供者之前读取了环境变量.

As far as I know there is no way to modify the environment variables before the configuration is actually loaded, because the configuration is loaded and reads the environment variables before the service providers are registered.

也请注意使用HTTP_HOST,因为它是客户端设置的标头,可能并不可靠.

Also betware of using HTTP_HOST as it's a client-set header and may not be reliable.

这篇关于如何在单个laravel应用中为多个域加载不同的.env文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆