mardi 25 juillet 2017

How to build a custom connection outside of database.php in Laravel 5.4

I am building a system that uses multiple databases. I currently have a custom config file which I am using that has some controls in it. This file is not put through version control.

I would like these databases to be independant of git. I am looking to build a custom connection without using config/database.php

I could of course remove config/database.php from git but I want to keep it neat and make use of my custom config file.

Here is my clientconfig.php to be found in /config folder

<?php
$clientDB = '';
if(isset($_SERVER['SERVER_NAME'])) {
    $apiDomain = $_SERVER['SERVER_NAME'];
    if ( $apiDomain == 'www.example.com' ) {
        $clientDB = 'clientdb_1';
    }
}
return  [

    'client_db' => $clientDB

];

I would like to add my connections in that file too that are found in database.php

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'example'),
        'username' => env('DB_USERNAME', 'example'),
        'password' => env('DB_PASSWORD', 'example'),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

    'clientdb_1' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => ('clientdb_1'),
        'username' => env('DB_USERNAME', 'example'),
        'password' => env('DB_PASSWORD', 'example'),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

Is there a clean way this can be done?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire