phariscope / multitenant
Multitenant components.
Installs: 43
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.2
- doctrine/cache: ^1.11 || ^2.0
- phariscope/safephp: ^1.0
- symfony/dotenv: ^5.4 || ^6.0 || ^7.0
- symfony/orm-pack: ^2.4 || ^5.4 || ^6.0 || ^7.0
- symfony/runtime: ^5.4 || ^6.0 || ^7.0
- symfony/yaml: ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- infection/extension-installer: 0.1.2
- infection/infection: ^0.29
- mikey179/vfsstream: ^1.6
- mockery/mockery: ^1.6
- phpstan/phpdoc-parser: ^1.30
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11
- squizlabs/php_codesniffer: 3.*
README
Easily add multitenancy capabilities to your Symfony projects without (too much) code modification.
Installation
Install the package using Composer:
composer require phariscope/multitenant
You can use Multitenant as a Symfony bundle. Simply add one line to your config/bundles.php
file:
return [ // other bundles Phariscope\MultiTenant\MultiTenantBundle::class => ['all' => true], ];
Usage
In a Symfony controller, follow these steps:
- Inject
EntityManagerResolver
into your controller’s constructor. - Retrieve the tenant-specific entity manager within your route action.
- create database and schema for a tenant if database does not exist for this tenant
- Enjoy...
For example, assuming you have a tenant_id
in your request or session:
class YourController extends AbstractController { public function __construct( private EntityManagerResolver $entityManagerResolver, ) {} #[Route('your/route', name: 'runYourRoute', methods: ['POST', 'GET'])] public function runYourRoute(Request $request): Response { $tenantEntityManager = $this->entityManagerResolver->getEntityManagerByRequest($request); (new DatabaseTools())->createDatabaseIfNotExists($entityManager); $repository = new YourSomeEntityDoctrineRepository($tenantEntityManager); // Your code here... } }
Creating a Tenant Database
Ensure you have the necessary console setup to handle tenant operations.
To create a database for a specific tenant (e.g., tenantID1234
), you can use the console command:
bin/console tenant:database:create tenantID1234
Creating a Schema for a tenant database
Once you have created a tenant database, you can create its schema.
You can use the console command:
bin/console tenant:schema:create tenantID1234