Download the PHP package wronx/multitenancy-bundle without Composer
On this page you can find all versions of the php package wronx/multitenancy-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wronx/multitenancy-bundle
More information about wronx/multitenancy-bundle
Files in wronx/multitenancy-bundle
Package multitenancy-bundle
Short Description Poor Man's MultiTenancy for Symfony
License WTFPL
Informations about the package multitenancy-bundle
Poor Man's Symfony Multitenancy Bundle
This piece of poorly written code may help you with creating multi-tenant applications in Symfony. Or may not. I don't know, I'm a plumber, not a fortune-teller.
License:
Copyright © 2017 github.com/WRonX This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
Features:
Just one: you can enable multi-tenant architecture in your application.
How it works
I couldn't think of better solution, so every tenant has a name and host, by which it's identified. Name is used to identify tenant when using Symfony console, host for everything else. In general, ConnectionWrapper
changes Connection
database, depending on currently used host.
NOTE: In performance matter, this is probably not the best idea for bigger applications and you probably should cache Connection somehow to prevent it from making tenant identifying SQL request every time it's created. But what do I know.
Installation and Configuration:
1. Installing the bundle
First, install the bundle with composer
:
Then add the bundle to AppKernel
:
And add the following do your config.yml
:
If you skip the last step, multitenancy will be disabled by default.
NOTE: With disabled multitenancy, your application uses the main (described in
parameters.yml
) database in normal way.
The following steps are assuming multitenancy is enabled.
2. Prepare Tenant Manager database:
First, create database, which will serve as Tenant Manager. That means connection details will be stored there. Passwords will be stored in plaintext, just like DB password in parameters.yml
. The parameters.yml
connection details should point to Tenant Manager database.
Now, Tenant Manager database should contain tenants
table with connection details for every tenant:
As ConnectionWrapper
uses REGEXP
, host field can look like the following:
.*
^(.*\.)*client01\.yourapplicationdomain\.com$
^((.*\.)*client02\.yourapplicationdomain\.com)|(clientsowndomain\.com)$
As you can see from this example, you can handle multime client domains, and subdomains can be ignored. Also, SELECT
query is ordered by host field's length (descending), so every request to non-existing host (tenant) will be handled by connection data defined in record with .*
host, which can be useful for creating demo environment.
Many thanks to swiniak for coming up with REGEXP
idea.
2. Using included code and configuration:
This should be self-explanatory.
3. Using Symfony console:
Just remember to add tenant name for every command, using --tenant=TENANTNAME
(this is the name field from tenants
table).
4. New console commands
Two new console commands were added:
tenants:list
just shows available tenant names (and some additional data)tenants:execute "command to execute"
executes given (quoted!) command on all tenants. Example:php app/console tenants:execute "doctrine:schema:update --dump-sql"
Changes coming soon
- adding console commands to automatically create tenants table and manage tenants
- changing ignored commands list into commands whitelist
Summary
Oh, come on, I spent enough time writing readme already...