Establish a database connection and language dynamically in Laravel

I have 3 domains pointing to the same Laravel application. I want each one to connect to its own database and upload its own TLD-based language file. What is the file where I can configure these settings? Can I do this directly in the configuration file or maybe some event before loading the configuration.

I have a short function that will analyze the domain and get a TLD, on the basis of which, after a quick check, we will know which database and language will be used.

+4
source share
1 answer

- . : https://laravel.com/docs/master/middleware

, , . , , . :

public function handle($request, Closure $next)
{
  $host = $request->getHost();

  //do your logic that determines the language and connection to use based on TLD
  $language = $this->getLanguageForTld($host);

  //set connection used
  Config::set('database.default', $language);

  //set application locale
  App::setLocale($language);

  return $next($request);
}
+6

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


All Articles