sbine/simple-tenancy
Composer 安装命令:
composer require sbine/simple-tenancy
包简介
Simple Laravel multi-tenancy in the same database
README 文档
README
Simple Tenancy adds automatic multi-tenant support to Eloquent models stored in a shared database.
It requires zero configuration in most cases, relying on established Laravel conventions and a single column on each table.
How it works
Under the hood, it has only 4 components:
Tenant: Keeps track of the current userHasTenancy: A trait for models belonging to the tenant, which registers:TenancyScope: A global scope limiting all the model's queries to the current tenantTenancyObserver: An observer which sets the current tenant column/identifier on save
By default, the tenant is Laravel's Auth::user(), and all tenancy checks are disabled when no one is authenticated.
Installation
- Install using Composer:
composer require sbine/simple-tenancy
- Add the
HasTenancytrait to all models belonging to the tenant:
class Account extends Model { use \Sbine\Tenancy\HasTenancy; }
- Ensure a
user_idcolumn exists on the table of every model using the trait.
Customizing the tenant column/ID
If needed, you can customize the name of the tenant column or identifier by extending the Tenant class and binding it into the container:
class MyTenant extends \Sbine\Tenancy\Tenant { /** * Retrieve the column identifying each model's tenant. */ public function column() { return 'tenant_hashid'; } /** * Retrieve the current tenant's identifier. */ public function id() { return $this->user->hashid; } }
Customizing tenancy behavior
By default, if no user is authenticated tenancy will be completely disabled. This is by design so the library doesn't interfere with testing, seeding, and other unauthenticated model usage.
To change the tenancy behavior in any way, you can override the Tenant binding in the application container.
For example, to prevent all querying and saving of tenant models when no one is authenticated:
// In AppServiceProvider.php public function register() { $this->app->singleton(\Sbine\Tenancy\Tenant::class, function () { // Throw an AuthenticationException if auth check fails return new \Sbine\Tenancy\Tenant(Auth::authenticate()); }); }
To allow overriding tenancy checks based on a policy or method, pass a callback returning true or false:
// In AppServiceProvider.php public function register() { $this->app->singleton(\Sbine\Tenancy\Tenant::class, function () { return new \Sbine\Tenancy\Tenant(Auth::user(), function ($user) { return $user->can('admin'); }); }); }
For complete control over tenant behavior, you can bind your own Tenant implementation:
// In AppServiceProvider.php public function register() { $this->app->singleton(\Sbine\Tenancy\Tenant::class, function () { return new MyTenant; }); }
Disabling tenancy
For convenience, a SuperAdmin class is provided which you can bind at any time to disable tenant checks:
$this->app->singleton(\Sbine\Tenancy\Tenant::class, \Sbine\Tenancy\SuperAdmin::class);
sbine/simple-tenancy 适用场景与选型建议
sbine/simple-tenancy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.63k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2020 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Simple」 「laravel」 「multi」 「tenancy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sbine/simple-tenancy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sbine/simple-tenancy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sbine/simple-tenancy 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Theme and asset management for laravel
This package provides Laravel bindings and a Eloquent/Model trait for pragmarx/recovery package.
A simple library for make Lexer and Parsers to build a language
A simple library that allows transform any kind of data to native php data or whatever
A simple library that uses the hydration pattern to fill objects given different sources of data.
统计信息
- 总下载量: 3.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-12