PHPackages                             hakam/multi-tenancy-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. hakam/multi-tenancy-bundle

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

hakam/multi-tenancy-bundle
==========================

Flexible and production-ready multi-tenancy bundle for Symfony.

v3.0.0(2mo ago)12439.6k↓23.4%36[3 PRs](https://github.com/RamyHakam/multi_tenancy_bundle/pulls)MITPHPPHP ^8.2CI passing

Since Apr 8Pushed 2mo ago7 watchersCompare

[ Source](https://github.com/RamyHakam/multi_tenancy_bundle)[ Packagist](https://packagist.org/packages/hakam/multi-tenancy-bundle)[ RSS](/packages/hakam-multi-tenancy-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (14)Versions (65)Used By (0)

Symfony Multi-Tenancy Bundle
============================

[](#symfony-multi-tenancy-bundle)

[![Multi-Tenancy Bundle (Desktop Wallpaper)](https://private-user-images.githubusercontent.com/17661342/237788633-eef23e6a-881c-4817-b7b8-8b7cec913154.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzQ2NzcwMTEsIm5iZiI6MTc3NDY3NjcxMSwicGF0aCI6Ii8xNzY2MTM0Mi8yMzc3ODg2MzMtZWVmMjNlNmEtODgxYy00ODE3LWI3YjgtOGI3Y2VjOTEzMTU0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjAzMjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwMzI4VDA1NDUxMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI3ZWU2NjUxZmZhOTQ0ZGU4ZDcxMjA2NDE1NjU1Y2RmMmQ0YWE1ZWYxZWExNGMwYjZhYjcwY2UxY2QxYTU2OWEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.yCw7C-BdtHGP2mmXScvzRfFoBL-WKFi8ovLtq54OblY)](https://private-user-images.githubusercontent.com/17661342/237788633-eef23e6a-881c-4817-b7b8-8b7cec913154.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzQ2NzcwMTEsIm5iZiI6MTc3NDY3NjcxMSwicGF0aCI6Ii8xNzY2MTM0Mi8yMzc3ODg2MzMtZWVmMjNlNmEtODgxYy00ODE3LWI3YjgtOGI3Y2VjOTEzMTU0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjAzMjglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwMzI4VDA1NDUxMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI3ZWU2NjUxZmZhOTQ0ZGU4ZDcxMjA2NDE1NjU1Y2RmMmQ0YWE1ZWYxZWExNGMwYjZhYjcwY2UxY2QxYTU2OWEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.yCw7C-BdtHGP2mmXScvzRfFoBL-WKFi8ovLtq54OblY)

[![Action Status](https://github.com/RamyHakam/multi_tenancy_bundle/workflows/action_status/badge.svg?style=flat-square)](https://github.com/RamyHakam/multi_tenancy_bundle/workflows/action_status/badge.svg?style=flat-square)[![Total Downloads](https://camo.githubusercontent.com/b89d5175f6affb54943d428d982293e663b94614a31b4bee79a2749715d65dd4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68616b616d2f6d756c74692d74656e616e63792d62756e646c653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hakam/multi-tenancy-bundle)[![Symfony Flex](https://camo.githubusercontent.com/0836726b0ad4afccc66980ec7c563d79bb4f8d6c5db800cacadae3939f495a86/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53796d666f6e79253230466c65782d526563697065253230417661696c61626c652d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/symfony/recipes-contrib)[![codecov](https://camo.githubusercontent.com/21436298ae613f2b408ea1972d0773d77a76bcc06ad1b2fe5fc0e0654e0bcd08/68747470733a2f2f636f6465636f762e696f2f67682f52616d7948616b616d2f6d756c74695f74656e616e63795f62756e646c652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/RamyHakam/multi_tenancy_bundle)

> 📚 **Full Documentation**: [Documentation](https://ramyhakam.github.io/multi_tenancy_bundle/intro/overview)

---

🧩 Overview
----------

[](#-overview)

The **Symfony Multi-Tenancy Bundle** enables scalable, production-ready multi-tenancy for Symfony applications.

Ideal for **SaaS platforms**, **region-based services**, and **multi-vendor e-commerce systems**, this bundle is built around a **database-per-tenant architecture**, giving each tenant:

- A fully isolated database
- Independent schema and migrations
- Configurable connection parameters (host, driver, credentials)

It integrates seamlessly with Doctrine and Symfony's service container, offering:

- Automatic tenant database switching at runtime via `SwitchDbEvent`
- **Automatic tenant resolution** from HTTP requests (subdomain, path, header, or custom)
- Separate migration and fixture paths for main vs. tenant databases
- Dedicated `TenantEntityManager` service for runtime isolation

For full usage examples and advanced configuration, see the [documentation](https://ramyhakam.github.io/multi_tenancy_bundle/).

---

🔄 Automatic Tenant Resolution (v3.0.0+)
---------------------------------------

[](#-automatic-tenant-resolution-v300)

Automatically resolve the current tenant from incoming HTTP requests — no manual event dispatching required.

### Supported Strategies

[](#supported-strategies)

StrategyExampleDescription`subdomain``tenant1.example.com`Extracts tenant from subdomain`path``/tenant1/dashboard`Extracts tenant from URL path segment`header``X-Tenant-ID: tenant1`Extracts tenant from HTTP header`host``client.com → tenant1`Maps full hostname to tenant`chain`Multiple strategiesTries resolvers in order with fallback### Quick Configuration

[](#quick-configuration)

```
# config/packages/hakam_multi_tenancy.yaml
hakam_multi_tenancy:
    resolver:
        enabled: true
        strategy: subdomain  # subdomain | path | header | host | chain
        options:
            base_domain: 'example.com'  # for subdomain strategy
```

### Strategy Examples

[](#strategy-examples)

**Subdomain-based** (e.g., `acme.myapp.com`):

```
resolver:
    enabled: true
    strategy: subdomain
    options:
        base_domain: 'myapp.com'
```

**Header-based** (for APIs):

```
resolver:
    enabled: true
    strategy: header
    options:
        header_name: 'X-Tenant-ID'
```

**Path-based** (e.g., `/acme/dashboard`):

```
resolver:
    enabled: true
    strategy: path
    excluded_paths: ['/api/public', '/health']
```

**Chain strategy** (fallback support):

```
resolver:
    enabled: true
    strategy: chain
    options:
        chain_order: [header, subdomain, path]
```

### Accessing Resolved Tenant

[](#accessing-resolved-tenant)

The resolved tenant ID is available in request attributes:

```
// In a controller
$tenantId = $request->attributes->get('_tenant');
```

### Custom Resolver

[](#custom-resolver)

Implement `TenantResolverInterface` for custom logic:

```
use Hakam\MultiTenancyBundle\Port\TenantResolverInterface;

class CustomResolver implements TenantResolverInterface
{
    public function resolve(Request $request): ?string
    {
        // Your custom logic
        return $request->cookies->get('tenant_id');
    }

    public function supports(Request $request): bool
    {
        return $request->cookies->has('tenant_id');
    }
}
```

> **Note:** Automatic resolution is disabled by default for backward compatibility. Manual `SwitchDbEvent` dispatching continues to work.

---

🚀 Quick Installation
--------------------

[](#-quick-installation)

### 1. Via Symfony Flex (Recommended)

[](#1-via-symfony-flex-recommended)

```
composer require hakam/multi-tenancy-bundle
```

Symfony Flex will automatically scaffold config, register the bundle, and create:

```
src/Entity/Main/
src/Entity/Tenant/
migrations/Main/
migrations/Tenant/

```

### 2. Manual Installation

[](#2-manual-installation)

```
composer require hakam/multi-tenancy-bundle
```

Then register in `config/bundles.php`, copy the example `hakam_multi_tenancy.yaml` from docs, and create the required directories.

---

📖 Examples
----------

[](#-examples)

The [`examples/`](examples/) directory contains **15 ready-to-use code examples** covering every feature of the bundle:

\#ExampleFeature01[Entity Setup](examples/01-entity-setup.php)`TenantDbConfig` entity with `TenantDbConfigTrait`02[Bundle Configuration](examples/02-bundle-configuration.php)Full YAML config reference03[Tenant Entities](examples/03-tenant-entities.php)Tenant-scoped entities (Product, Order)04[Database Lifecycle](examples/04-database-lifecycle.php)Create DB, switch, CRUD, CLI commands05[Tenant Migrations](examples/05-tenant-migrations.php)Platform-agnostic migrations with Schema API06[Resolvers](examples/06-resolvers.php)All 5 resolution strategies + controller usage07[Lifecycle Events](examples/07-events.php)All 6 events with subscriber pattern08[Custom Config Provider](examples/08-custom-config-provider.php)Redis, static, and in-memory providers09[Tenant Fixtures](examples/09-tenant-fixtures.php)`#[TenantFixture]` attribute + CLI10[Cache Isolation](examples/10-tenant-aware-cache.php)`TenantAwareCacheDecorator` usage11[Tenant Context](examples/11-tenant-context.php)`TenantContextInterface` in services, Twig, listeners12[Testing](examples/12-testing.php)`TenantTestTrait` with `runInTenant()`13[Shared Entities](examples/13-shared-entities.php)`#[TenantShared]` with exclusions14[Custom Resolver](examples/14-custom-resolver.php)JWT, query param, API key resolvers15[Full Onboarding Flow](examples/15-full-onboarding-flow.php)Complete end-to-end tenant onboarding> See also the [Examples documentation page](https://ramyhakam.github.io/multi_tenancy_bundle/examples/examples) for detailed explanations.

---

🔗 Useful Links
--------------

[](#-useful-links)

- **Full Documentation:** \[[https://ramyhakam.github.io/multi\_tenancy\_bundle/](https://ramyhakam.github.io/multi_tenancy_bundle/)\]
- **GitHub:** [https://github.com/RamyHakam/multi\_tenancy\_bundle](https://github.com/RamyHakam/multi_tenancy_bundle)
- **Packagist:**
- **Example Project:**

---

📄 License
---------

[](#-license)

MIT © Ramy Hakam

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity47

Moderate usage in the ecosystem

Community24

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 81.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~65 days

Recently: every ~27 days

Total

28

Last Release

86d ago

Major Versions

v0.2 → v1.0.02022-05-28

v1.0.3 → v2.0.02023-04-24

v2.9.4 → v3.0.02026-02-21

PHP version history (9 changes)v0.2PHP ^7.2.5

v1.0.0PHP ^7.4

v1.0.1PHP ^7.4 | ^8.0

v1.0.2PHP ^7.4 | ^8.0 | ^8.1

v2.0.0PHP  ^8.0 | ^8.1

v2.5.1.x-devPHP ^8.1|^8.2

v2.6.0PHP ^8.1|^8.2|^8.3

v2.9.0PHP ^8.1|^8.2|^8.3|^8.4

v3.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/05d31fae310da7c391a750a6c7fce558786e735eff7ff312f062cc45f12f2466?d=identicon)[RamyHakam](/maintainers/RamyHakam)

---

Top Contributors

[![RamyHakam](https://avatars.githubusercontent.com/u/17661342?v=4)](https://github.com/RamyHakam "RamyHakam (192 commits)")[![mogilvie](https://avatars.githubusercontent.com/u/7894754?v=4)](https://github.com/mogilvie "mogilvie (29 commits)")[![Tbeaumont79](https://avatars.githubusercontent.com/u/32023386?v=4)](https://github.com/Tbeaumont79 "Tbeaumont79 (4 commits)")[![skarnl](https://avatars.githubusercontent.com/u/3471506?v=4)](https://github.com/skarnl "skarnl (2 commits)")[![Galironfydar](https://avatars.githubusercontent.com/u/13006625?v=4)](https://github.com/Galironfydar "Galironfydar (2 commits)")[![JensMyParcel](https://avatars.githubusercontent.com/u/125662333?v=4)](https://github.com/JensMyParcel "JensMyParcel (1 commits)")[![NikoGrano](https://avatars.githubusercontent.com/u/4658966?v=4)](https://github.com/NikoGrano "NikoGrano (1 commits)")[![robertfausk](https://avatars.githubusercontent.com/u/1651297?v=4)](https://github.com/robertfausk "robertfausk (1 commits)")[![joosee7](https://avatars.githubusercontent.com/u/83545461?v=4)](https://github.com/joosee7 "joosee7 (1 commits)")[![borutainfo](https://avatars.githubusercontent.com/u/16668386?v=4)](https://github.com/borutainfo "borutainfo (1 commits)")[![michielkalle](https://avatars.githubusercontent.com/u/14213930?v=4)](https://github.com/michielkalle "michielkalle (1 commits)")

---

Tags

doctrinemulti-tenancymulti-tenantmulti-vendorphpsymfonysymfony-bundlesymfony4symfony5symfony6

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hakam-multi-tenancy-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/hakam-multi-tenancy-bundle/health.svg)](https://phpackages.com/packages/hakam-multi-tenancy-bundle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
