PHPackages                             abbotton/think-tenancy - 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. abbotton/think-tenancy

ActiveLibrary

abbotton/think-tenancy
======================

基于ThinkPHP6开发的多租户SAAS扩展

19274[2 issues](https://github.com/Abbotton/think-tenancy/issues)PHP

Since Apr 24Pushed 4y ago2 watchersCompare

[ Source](https://github.com/Abbotton/think-tenancy)[ Packagist](https://packagist.org/packages/abbotton/think-tenancy)[ RSS](/packages/abbotton-think-tenancy/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

基于ThinkPHP6开发的多租户扩展
===================

[](#基于thinkphp6开发的多租户扩展)

 [![](https://camo.githubusercontent.com/a6f131caec0f995618c7715729e8e02480d4877a694fa343d3369a50fb9d20b9/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f5468696e6b25323054656e616e63792e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6162626f74746f6e2532467468696e6b2d74656e616e6379267061747465726e3d666c6f6174696e67436f6773267374796c653d7374796c655f31266465736372697074696f6e3d4d756c7469706c652b74656e616e63792b666f722b5468696e6b504850266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d736572766572)](https://camo.githubusercontent.com/a6f131caec0f995618c7715729e8e02480d4877a694fa343d3369a50fb9d20b9/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f5468696e6b25323054656e616e63792e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6162626f74746f6e2532467468696e6b2d74656e616e6379267061747465726e3d666c6f6174696e67436f6773267374796c653d7374796c655f31266465736372697074696f6e3d4d756c7469706c652b74656e616e63792b666f722b5468696e6b504850266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d736572766572)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b827b35fcfd62ad5d1e0d4513b9c2f82eabac3763f7f3b44ef3fad132fa4f84f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6162626f74746f6e2f7468696e6b2d74656e616e63792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/abbotton/think-tenancy)[![Total Downloads](https://camo.githubusercontent.com/728c1bf0c4a1bd2e660fa12c2df9f7b037fea1bbfca10545789e872e088ceb9f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6162626f74746f6e2f7468696e6b2d74656e616e63792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/abbotton/think-tenancy)[![GitHub Actions](https://github.com/abbotton/think-tenancy/actions/workflows/main.yml/badge.svg)](https://github.com/abbotton/think-tenancy/actions/workflows/main.yml/badge.svg)

`无侵入`、`简单易上手`、`高度可自定义`的`ThinkPHP v6.x` 多租户扩展。

### 功能特性

[](#功能特性)

- 通过`子域名`识别租户，实现租户`数据库`、`文件存储`、`缓存`、`Session`、`Cookie`、`视图`的隔离；
- 可自定义`租户`租户模型以及字段名称；
- 完善的`事件`机制，通过监听或者订阅事件，可实现更多自定义的需求；
- 扩展`数据迁移`和`数据填充`指令，可以方便的维护租户的`数据迁移`和`数据填充`；
- 高覆盖率的单元测试（WIP）；

### 安装

[](#安装)

准备工作：

- 创建`ThinkPHP v6.x`项目；
- 完成泛域名解析；

开始安装：

```
# 引入扩展
composer require abbotton/think-tenancy
# 发布数据迁移文件，发布后的文件位于`database/migrations`文件夹中
php think tenants:publish
```

### 使用

[](#使用)

#### 单数据库模式：

[](#单数据库模式)

- 修改`config/tenancy.php`以及`database/migrations/20220422082201_add_tenants_table.php`中的相关配置；
- 执行数据迁移，创建租户信息表：`php think migrate:run`；
- 自主完善租户信息的创建逻辑；
- 监听`think\tenancy\events\TenantCreated::class`事件进行自定义操作：
    - 创建租户`OSS Bucket`；
    - 向租户发送邮件通知；
    - ......
- 访问租户对应域名验证租户信息是否正确；

#### 多数据库模式：

[](#多数据库模式)

- 修改`config/tenancy.php`以及`database/migrations/20220422082201_add_tenants_table.php`中的相关配置；
- 执行数据迁移，创建租户信息表：`php think migrate:run`；
- 自主完善租户信息的创建逻辑；
- 通过`php think tenants:migrate:create`命令创建租户的数据迁移文件；
- 通过`php think tenants:seed:create`命令创建租户的数据填充文件；
- 监听`think\tenancy\events\TenantCreated::class`事件进行自定义操作：
    - 创建租户数据库；
    - 创建租户数据库用户并授权；
    - 通过`php think tenants:migrate:run --tenant=sub_domain`执行租户数据迁移；
    - 通过`php think tenants:seed:run --tenant=sub_domain`执行租户数据填充；
    - 创建租户`OSS Bucket`；
    - 向租户发送邮件通知；
    - ......
- 访问租户对应域名验证租户信息是否正确；

### 命令行

[](#命令行)

#### 基本说明

[](#基本说明)

- 租户的`数据迁移`文件默认存放在`database/migrations/tenants`文件夹中，可修改`config/tenancy.php`自定义，也可以通过`path`参数指定；
- 租户的`数据填充`文件默认存放在`database/seeds/tenants`文件夹中，可修改`config/tenancy.php`自定义，也可以通过`path`参数指定；
- `path`参数传参需传入绝对路径；
- `--tenant`选项需要传入租户的**二级域名标识**，否则会因为匹配不到租户而报错；

#### 命令列表

[](#命令列表)

```
# 创建租户迁移文件
tenants:migrate:create  []

# 执行租户数据迁移
tenants:migrate:run [options] [--] []

# 执行租户数据迁移回滚
tenants:migrate:rollback [options] [--] []

# 查看租户数据迁移状态
tenants:migrate:rollback [options] [--] []

# 创建租户数据填充文件
tenants:seed:create  []

# 执行租户数据填充
tenants:seed:run [options] [--] []

# 发布租户表数据迁移文件
tenants:publish
```

### 测试(WIP)

[](#测试wip)

```
composer test
```

### 更新日志

[](#更新日志)

请查看 [CHANGELOG](CHANGELOG.md) 获取更多信息.

### 致谢

[](#致谢)

- [Abbotton](https://github.com/Abbotton)
- [ThinkPHP](https://github.com/top-think/think)
- [Tenancy for Laravel](https://github.com/archtechx/tenancy)

### License

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16308633?v=4)[Abbotton](/maintainers/Abbotton)[@Abbotton](https://github.com/Abbotton)

---

Top Contributors

[![Abbotton](https://avatars.githubusercontent.com/u/16308633?v=4)](https://github.com/Abbotton "Abbotton (24 commits)")

---

Tags

saastenancythinkphpthinkphp-saasthinkphp-tenancythinkphp6

### Embed Badge

![Health badge](/badges/abbotton-think-tenancy/health.svg)

```
[![Health](https://phpackages.com/badges/abbotton-think-tenancy/health.svg)](https://phpackages.com/packages/abbotton-think-tenancy)
```

PHPackages © 2026

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