Have you watched the multi-tenant video series right here on laracasts.
Laravel Multitenant solution.
Hello all,
I've been looking up information about multi tenancy (MT) in Laravel for the past few days. I've come across a whole bunch of heavy swiss army knife solutions that can handle almost anything. Yet they impact your codebase heavily. I've found a excellent video about multi tenancy by Mohamed Said.
"I will use one laravel install to service multiple tenants based on subdomain or domain names"
In this video he explains a lean and lightweight approach for MT in Laravel. His approach featured one database connection for the landlord and one for the tenants databases. This works if all databases for tenants are hosted on the same db server. Unfortunately most shared hostings create a new database on a different host, thus the entire connection needs to be changed per client. And here is where my issue starts.
So when a tenant visits his subdomain of the app the correct connection to the correct database must be established. The function below handles that:
I've created a separate .env file for eacht tenant (.env.tenant.tenantname). I use the Dotenv library to parse and load the needed variables from the custom .env file and fill the connection data with the values from the env variables. Then purge the 'tenant' connection and reconnect. I also try to change the APP_KEY for each tenant (in case they store some sensitive data).
public function configure()
{
$dotenv = Dotenv::createUnsafeMutable(base_path(),'.env.tenant.'.$this->name);
$dotenv->load();
config([
'database.connections.tenant.host' => $_ENV['TENANT_DB_HOST'],
'database.connections.tenant.database' => $_ENV['TENANT_DB_DATABASE'],
'database.connections.tenant.username' => $_ENV['TENANT_DB_USERNAME'],
'database.connections.tenant.password' => $_ENV['TENANT_DB_PASSWORD'],
'database.connections.tenant.port' => $_ENV['TENANT_DB_PORT'],
'app.key' => $_ENV['TENANT_KEY']
]);
DB::purge('tenant');
DB::reconnect('tenant');
Schema::connection('tenant')->getConnection()->reconnect();
return $this;
}
Since i'm new to Laravel and don't know it's inner workings that well, i've read up on Laravel enviroment files en config. What i understand from the documentation is that the config is cahced for optimisation and must be cleared on deploy.
But what happens if i change the config app.key and other variables on the fly? Will this cause issues for the tenants? Data stored in wrong database or encrypted with wrong key?
Each tenant will also have a api that will be open to receive incoming data, lets say for example 1200+ people push data to 4 different tenants at the same moment. Can the caching break the app and corrupt the data per tenant?
Also is the approach of loading the data out of custom .env files a good solution or should i move to a database driven approach?
At this point i've spend days reading up and trying to come up with a solution but the dynamic programatically changing of the application config (esp. the app key) during high traffic is somewhat mindboggling.
Looking forward on reading your opinion and advice on this matter.
Best regards. Bart
Please or to participate in this conversation.