PHP - How to configure Laravel .env to access different databases and different languages?

1

I'm working on a system (portal) that offers service to 19 countries. For each country there is a database and each country with its language dictionary (even countries with the same language, have dicinários diferenets EX: Mexico and Spain). The system was built in PHP, IIS windows 7. I am wanting to migrate the system to php - laravel, apache, linux. The first challenge I'm encountering is the configuration of the .avi's .lv file (latest version). It supports a language and a database.

Enntão logic is the segunte: when the user writes br.portal.mycompany.com the system must retrieve the access credentials br_portal database and TBM recover the labels in Portuguese (br) in the dictionary database .

How could I configure env to accept different credentials for different databases and different languages based on different url possibilities? Eg br.portal.mycompany.com, us.portal.mycompany.com, es.portal.mycompany.com, etc ...

    
asked by anonymous 29.06.2016 / 18:16

1 answer

1

You can create different connections.

In the config / databases.php file there is the default bank connection, but you can create other connections. So:

'connections' => [

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'mysql2' => [ // Aqui você define o nome da conexão
            'driver' => 'mysql',
            'host' => env('DB_BR_HOST', 'localhost'),
            'port' => env('DB_BR_PORT', '3306'),
            'database' => env('DB_BR_DATABASE', 'forge'),
            'username' => env('DB_BR_USERNAME', 'forge'),
            'password' => env('DB_BR_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

    ],

Then in the .env file you put the data for access. So:

DB_CONNECTION=mysql // Banco padrão
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=


DB_BR_CONNECTION=mysql // Outros bancos
DB_BR_HOST=localhost
DB_BR_PORT=3306
DB_BR_DATABASE=database
DB_BR_USERNAME=root
DB_BR_PASSWORD=

And to make an appointment at the bank you need to define which connection the application should use. So:

$sql = DB::connection('name_connection')->table('name_table')->get();
    
14.09.2016 / 15:47