I recently upgraded our organizations symfony 3.4 app to symfony 5.4.6 Flex.I have gotten all of the main functions of the application working, although there is one thing that I have been putting off, as its just escaping me on how to do it.
Multi Tenant ParametersOur old 3.4 Symfony application would be able to intercept the $_SERVER['HTTP_HOST'] and use the first part of the url to determine what set of parameters to load. for example
main.example.com would load config/main/paramters.yamlother.example.com would load config/other/parameters.yamla third non triggered set of configs "config/default/parameters.yaml" were established so that bin/console commands would still work for terminal work.
Each config file would have organization specific parameters such as DB info, company name, etc.That way we could have one application, two organizations using it, separated database information for ultimate privacy.
I think it was about the symfony 5.1 mile marker where i noticed that the kernel file changed to remove a lot of that granular control of which config files you could load.
I have modified the V5.4.6 Kernel to work, but for some reason, its not working 100% of the time.Here is my code
namespace App;use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;use Symfony\Component\DependencyInjection\ContainerBuilder;use Symfony\Component\HttpKernel\Kernel as BaseKernel;class Kernel extends BaseKernel{ use MicroKernelTrait; public function __construct(string $environment, bool $debug) { parent::__construct($environment, $debug); } protected function build(ContainerBuilder $container) { $loader = $this->getContainerLoader($container); $configFolder = 'default'; if (isset($_SERVER['HTTP_HOST'])) { $host = explode('.', $_SERVER['HTTP_HOST']); if ($host[0] === 'main' or $host[0] === 'main-staging') $configFolder = 'main'; else $configFolder = $host[0]; //loads other organizations 'other.example.com' } $loader->load($container->getParameter('kernel.project_dir') . '/multi-tenant/'. $configFolder . '/config.yaml'); parent::build($container); }}
So this totally works. Say I got to main.example.com, it loads all the main organizations parameters. But if I then open up an incognito tab and go to other.example.com it loads the main organizations parameters. It keeps this behavior till I clear the cache or let it sit for a while.
Otherwise if i start by opening a tab to other.example.com, it loads the other organizations config file, and if I open it in an incognito tab and navigate to main.example.com, it won't load the main configs unless I clear the cache or let it sit for a while.
EDITI have left the index.php in public folder alone:
<?phpuse App\Kernel;require_once dirname(__DIR__).'/vendor/autoload_runtime.php';return function (array $context) { return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);};
Some of the other things I have attempted to try and resolve this issue are:
- disable app / system caching
- disable Twig caching as those are the easiest identifiers if the right parameters loaded.
Thank you for any advise you can give.