I would like to access single laravel application by multiple domain.
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);
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); }
but still i am not getting correct domain url using url(config('app.url'))
How to load all .env
variable overwrite for each domain?
Advertisement
Answer
config()
sets the configuration settings but not the environment settings.
You have to do the following:
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.
Also betware of using HTTP_HOST
as it’s a client-set header and may not be reliable.