定制 asseco-voice/laravel-multitenancy 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  • migrate
  • db:seed
  • db: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_resolver is the class responsible for figuring out who the current tenant is. Default resolution is by a domain. You can create a new class implementing TenantResolver interface and use it instead of the default one.

  • tenant_model can be overridden, just be sure to extend the default Tenant model from the package.

  • switch_tenant_tasks is an array of classes implementing SwitchTenantTask interface 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 asseco-voice/laravel-multitenancy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-30