At the moment I'm implementing a module for managing multi-site sites

It would be desirable to implement switching a choice of a site in backend and accordingly change settings database connection on the fly


Sites /

Default /

Site1 /

Site2 /

The work is done on the default site. After selecting in the admin panel - "work at site1", its settings are loaded in default on the fly without changing the settings.php. In particular, only the connection settings of the database change

Tried the following methods

  1. Changing the active connection according to the default-extra scheme

    \ Drupal \ Core \ Database \ Database :: setActiveConnection ('extra');

    Did not work out

  2. I tried at the initialization level to place a call at the initialization level

    Did not work out

Is it possible to do this? There is an idea on putting a variable in a session and in settings.php checking it. But how to access it from settings.php?


The best way to do this is to add multiple databases to your settings.php and then switch which DB you connect to, but since you mentioned that you tried this already and didn't have success, you can try adding all of your database settings into your sites/default/settings.php inside of switch statement which looks at something (anything) persistent that you change. For example, you can write to a local file and read it in settings.php.

This is hacky but since you are looking for alternatives to the more accepted solution...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.