Thanks! Such solution too is necessary to me. I will observe.
Jan 16, 2016
11
Level 11
[Tip] Single App - Multi Dbs and Domains
Hi All,
I am developing a application and... I want to share my solution with you...
Run this in your console
php artisan make:provider MultiAppServiceProvider
Open your new service provider and replace your boot method on it...
/**
* Bootstrap the application services.
*
* @throws DomainNotFoundException
*/
public function boot()
{
if ( ! \App::runningInConsole()) {
$domain = \Request::server('HTTP_HOST');
$file = str_slug(str_replace('.', '_', $domain), '_') . '.php';
// if config file not found throw exception and event
if ( ! file_exists(config_path('sites/') . $file)) {
$msg = trans('system.domainNotFound');
// event(new DomainNotFound); // you can announce an event...
throw new DomainNotFoundException($msg);
}
// get settings by domain
$settings = require config_path('sites/') . $file;
// set all settings by request domain
foreach (array_keys((array)$settings) as $key) {
$replace = array_replace_recursive(config($key), $settings[$key]);
config([$key => $replace]);
}
}
}
Add service provider to app/config/app.php
App\Providers\MultiAppServiceProvider::class,
Create a directory in app/config/sites ...
return [
// site settings
'app' => [
'site_name' => 'Site Name',
'url' => 'http://example.dev',
'school_count' => 2,
],
// db settings
'database' => [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'example_dev',
'username' => 'root',
'password' => 'pass',
]
],
],
];
You can add any key, values as you wish...
I hope you like it... Its works for me...
Any comments are appreciated...
Thanks
Please or to participate in this conversation.