Multi tenant with separate database

Hi all. I would like to design a multi tenant api. Each tenant can have multiple users. Each tenant has his own database. How can i handle that with Flow framework. Thanks.

You can use different FLOW_CONTEXTS like Production/CustomerA vs Production/CustomerB to achieve something like that … be careful this is not a trivial adjustment.

Flow will load all Configurations from Generic to specific subContext so Configuration/Production/CustomerA/Settings.yaml will override Configuration/Production/Settings.yaml .

…or use separate instances?!

martoro thanks for your answer. I must admit that i am a new user of Flow. I am a symfony user but the ddd aspect of your framework is what draw me to it. In synfony i was changing the entity manager of doctrine dynamically on every request.
Please can you provide a code tip or a tutorial link?

How can i achieve that dynamicaly for every tenant?

Maybe I misunderstood. But if you require a separate database for every tenant, shouldn’t it be possible to add a separate virtual host, too?

bwaidelich thanks for your concern. Yes i can do that. But i want the tenant to be created dynamically in a registration form and the same code will be used for all tenants. If i can switch dynamically the flow context (as martoro proposed) based on request that would fantastic. So i won’t touch the webserver config each time a tenant is registrered. Those server configs are critical i guess. Also use the same code guarantees that i have one code to update when new features are developed.

For different FLOW_CONTEXTs you would have to define the environment variable FLOW_CONTEXT as needed for the request. This allows to adjust everything you can adjust via setting. Downside is that you have to set FLOW_CONTEXT on the cli accordingly which can easily lead to errors.

Not sure i would use that to create tenants dynamically.

So you create database instances dynamically, too? (just being curious)

punktde/sitespecifics - Packagist might also be an option for you (I didn’t look into it closely)

It is fine for me. But how can i set the context dynamically in a http request? I can see in the docs how i can do it in cli request but not in an http request. I don’t want to tweak the weberver config.

you can try to hack into your index.php and use all kinds of dark magic :wink:

i dont know exacly what your plan is but i did something similar for e2e testing:

(would NOT recommend to do this 1by1 (as this way the client can define any context, which is horrible but that should be a start)

$context = \Neos\Flow\Core\Bootstrap::getEnvironmentConfigurationSetting('FLOW_CONTEXT') ?: 'Development';
$context .= (isset($_SERVER['HTTP_FLOWSUBCONTEXT']) ? '/' . $_SERVER['HTTP_FLOWSUBCONTEXT'] : '');
$bootstrap = new \Neos\Flow\Core\Bootstrap($context, $composerAutoloader);
$bootstrap->run();

Thanks Marc for your code. Is there a way to alter the content of a context by code in order to replace some entries dynamically before applying it?

flows context are mostly static. The most dynamic thing is to use enviroment variables inside of a settings value (see docs)

Marc thanks for your answer. I will use a setting yaml file with env variables and use Php putenv function to set the appropriate database on every request… I will be good if can pass a class variables into an config yaml file…