How to set up and run multiple websites with corresponding databases with one instance of Laravel code

I have a script that I want to implement in Laravel.

I have already implemented this approach in custom PHP. But this time I want to move all websites to frames (Laravel).

I have several domains for different regions ( Localization), each domain has its own database, and the whole structure of the database tables is the same, only the data is different.

Now I want to use one instance of Laravel code with the ability to connect multiple databases with multiple domains, and each domain has its own theme files.

Suppose I have some domains

 1. abc.com and it has database with name of db_abc with theme/template ABC
 2. xyz.com and it has database with name of db_xyz with theme/template XYZ

When the domain abc.comgets / gets access, I want to connect to the DB db_abcand upload the data to ABC theme/template. Similarly, when it xyz.comgets / gets access to the database connection, you need to do it with db_xyz, and the data should be loaded into files theme/template XYZ.

Please help me in this regard.

Thank.

+4
source share
1 answer

First of all, you need to specify all domains on one project.

After that, you can catchdomain with a group of routes:

Route::group(['domain' => 'abc.com'], function() {
    Route::get('/', 'SomeController@someAction');
});

To use multiple databases, you can use connection():

$users = DB::connection('foo')->select(...);

, (, , ).

, :

if (strpos(request()->server('HTTP_HOST'), 'abc.com')) {
    session(['site' => 'abc']);
}

config :

$currentDb = config('sites.db')[session('site')];
$currentViewsDir = config('sites.views')[session('site')];
+1

Source: https://habr.com/ru/post/1653402/


All Articles