asseco-voice/laravel-multitenancy
Composer 安装命令:
composer require asseco-voice/laravel-multitenancy
包简介
Laravel multitenancy support
README 文档
README
Multitenancy
Purpose of this repository is to enable multitenancy with multiple DB setup.
Installation
Require the package with composer require asseco-voice/laravel-multitenancy.
Service provider will be registered automatically.
Setup
DB setup
Since this is a multi-DB tenancy package be sure to provide separate env values for landlord connection as well as connection for tenants.
Landlord is a separate DB connection which holds all shared migrations. The main one given through the
package is tenants table which will hold configuration for all the tenants in the system.
Add Landlord env variables (format is the same as native DB connection):
LANDLORD_CONNECTION=
LANDLORD_HOST=
LANDLORD_PORT=
LANDLORD_DATABASE=
LANDLORD_USERNAME=
LANDLORD_PASSWORD=
Use native DB connection to provide connection for tenants but comment out DB_DATABASE as
this will be dynamically switched during runtime.
DB_CONNECTION=
DB_HOST=
DB_PORT=
# DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
This means that all the tenants will be on a single connection but on separate databases. If you want to provide custom connection for some (or all) tenants, you can do so by providing that config within a DB table. More on that later.
Tenant aware commands
Some native Artisan commands are modified to provide two additional options:
--tenant- where you can provide a tenant ID for command to run on (i.e.--tenant=1). If you omit this argument, the command will run for all tenants.--landlord- will assume to run on landlord connection.
Commands currently available are:
migratedb:seeddb:wipe
Migrations
Publish the migration by running:
php artisan vendor:publish --tag="asseco-multitenancy-migrations"
This will create a single migration within a migrations/landlord directory. Landlord
represents a shared set of migrations which should be accessible independently of a chosen
Tenant.
The default migration (tenants table) should hold all data related to tenants in the system.
You are free to add other migrations there which would represent a shared set of tables across
all tenants.
Migrate landlord files by running php artisan migrate --landlord
Once you have a tenants table, you can add a new tenant by manually filling out the DB or
using Tinker (TODO: API + auto create). At that point, be sure to also create the database you
specified in the table.
Example:
-------------------------------------
ID Name Domain Database
-------------------------------------
1 Foo foo.localhost foo
Be sure that foo database exists on the server you defined within .env. At the point you
hit foo.localhost, you will see the tenant switch to that particular DB.
Separate tenant connections
To provide a tenant with a completely custom DB connection edit its configuration in DB by filling
out all the db_ values in the tenants table. If one of the values is missing from the configuration,
default will be used. Rest of the values will in that case be ignored.
Extending the package
Publish the configuration file by running:
php artisan vendor:publish --tag="asseco-multitenancy-config"
Extensible parts:
-
tenant_resolveris the class responsible for figuring out who the current tenant is. Default resolution is by a domain. You can create a new class implementingTenantResolverinterface and use it instead of the default one. -
tenant_modelcan be overridden, just be sure to extend the defaultTenantmodel from the package. -
switch_tenant_tasksis an array of classes implementingSwitchTenantTaskinterface which are being ran when tenant is switched.
Final notes
Due to our specific use cases, this package was created as a merge of these 2 amazing multi tenancy packages:
asseco-voice/laravel-multitenancy 适用场景与选型建议
asseco-voice/laravel-multitenancy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「laravel」 「multitenancy」 「microservices」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 asseco-voice/laravel-multitenancy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 asseco-voice/laravel-multitenancy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 asseco-voice/laravel-multitenancy 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
PHP module for MySql database
Alfabank REST API integration
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-30
