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.1.0(2mo ago)12946.3k↓42%38[3 issues](https://github.com/RamyHakam/multi_tenancy_bundle/issues)[3 PRs](https://github.com/RamyHakam/multi_tenancy_bundle/pulls)MITPHPPHP ^8.1CI passing

Since Apr 8Pushed 2mo ago8 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 2d ago

READMEChangelog (10)Dependencies (28)Versions (67)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.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3ODI4NDIyNDIsIm5iZiI6MTc4Mjg0MTk0MiwicGF0aCI6Ii8xNzY2MTM0Mi8yMzc3ODg2MzMtZWVmMjNlNmEtODgxYy00ODE3LWI3YjgtOGI3Y2VjOTEzMTU0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLN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tdHlwZT1pbWFnZSUyRnBuZyJ9.lVBfGPxg7U4iVi83Jg4WHVXQf2-UOuxP6wnc5VQW0JQ)

[![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/)

---

🧩 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

Maintenance86

Actively maintained with recent releases

Popularity48

Moderate usage in the ecosystem

Community24

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 81.5% 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 ~63 days

Recently: every ~17 days

Total

30

Last Release

69d 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 (10 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

v3.0.1PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17661342?v=4)[Ramy Hakam](/maintainers/RamyHakam)[@RamyHakam](https://github.com/RamyHakam)

---

Top Contributors

[![RamyHakam](https://avatars.githubusercontent.com/u/17661342?v=4)](https://github.com/RamyHakam "RamyHakam (194 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)")[![michielkalle](https://avatars.githubusercontent.com/u/14213930?v=4)](https://github.com/michielkalle "michielkalle (1 commits)")[![NikoGrano](https://avatars.githubusercontent.com/u/4658966?v=4)](https://github.com/NikoGrano "NikoGrano (1 commits)")[![dfranco](https://avatars.githubusercontent.com/u/482177?v=4)](https://github.com/dfranco "dfranco (1 commits)")[![robertfausk](https://avatars.githubusercontent.com/u/1651297?v=4)](https://github.com/robertfausk "robertfausk (1 commits)")[![JensMyParcel](https://avatars.githubusercontent.com/u/125662333?v=4)](https://github.com/JensMyParcel "JensMyParcel (1 commits)")[![borutainfo](https://avatars.githubusercontent.com/u/16668386?v=4)](https://github.com/borutainfo "borutainfo (1 commits)")[![joosee7](https://avatars.githubusercontent.com/u/83545461?v=4)](https://github.com/joosee7 "joosee7 (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

[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.9M388](/packages/easycorp-easyadmin-bundle)[sylius/sylius

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

8.5k5.9M738](/packages/sylius-sylius)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

9421.6k61](/packages/open-dxp-opendxp)[sulu/sulu

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

1.3k1.4M203](/packages/sulu-sulu)[pimcore/pimcore

Content &amp; Product Management Framework (CMS/PIM/E-Commerce)

3.8k3.8M508](/packages/pimcore-pimcore)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M574](/packages/shopware-core)

PHPackages © 2026

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