nunomazer/laravel-samehouse 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

nunomazer/laravel-samehouse

Composer 安装命令:

composer require nunomazer/laravel-samehouse

包简介

A multi-tenant Laravel package, based on single database, simple and ease to use

README 文档

README

Latest Version on Packagist Total Downloads Software License

laravel-samehouse

for Laravel & Lumen 5.2+

A single database multi-tenancy package for Laravel (5.2 -13.x) & Lumen version 5.2.

NOTE: this package is based on HipsterJazzbo/Landlord and Torzer/awesome-landlord. This version is a fork from https://github.com/torzer/awesome-landlord that was archived.

Installation

To get started, require this package:

composer require nunomazer/laravel-samehouse

Laravel

Add the ServiceProvider in config/app.php:

    'providers' => [
        ...
        NunoMazer\Samehouse\LandlordServiceProvider::class,
    ],

Register the Facade if you’d like:

    'aliases' => [
        ...
        'Landlord'   => NunoMazer\Samehouse\Facades\Landlord::class,
    ],

You could also publish the config file:

php artisan vendor:publish --provider="NunoMazer\Samehouse\LandlordServiceProvider"

and set your default_tenant_columns setting, if you have an app-wide default. LandLord will use this setting to scope models that don’t have a $tenantColumns property set.

Lumen

You'll need to set the service provider in your bootstrap/app.php:

$app->register(NunoMazer\Samehouse\LandlordServiceProvider::class);

And make sure you've un-commented $app->withEloquent().

Usage

This package assumes that you have at least one column on all of your Tenant scoped tables that references which tenant each row belongs to.

For example, you might have a companies table, and a bunch of other tables that have a company_id column.

Adding and Removing Tenants

IMPORTANT NOTE: Laravel Samehouse is stateless. This means that when you call addTenant(), it will only scope the current request.

Make sure that you are adding your tenants in such a way that it happens on every request, and before you need Models scoped, like in a middleware or as part of a stateless authentication method like OAuth.

You can tell Landlord to automatically scope by a given Tenant by calling addTenant(), either from the Landlord facade, or by injecting an instance of TenantManager().

You can pass in either a tenant column and id:

Landlord::addTenant('tenant_id', 1);

Or an instance of a Tenant model:

$tenant = Tenant::find(1);

Landlord::addTenant($tenant);

If you pass a Model instance, Landlord will use Eloquent’s getForeignKey() method to decide the tenant column name.

You can add as many tenants as you need to, however Landlord will only allow one of each type of tenant at a time.

Following is a sample middleware to implement the process of setting tenant on every request.

First create the class:

php artisan make:middleware SetTenant

Then, code it to set the tenant id for authenticated user, you can user the following example:

<?php

namespace App\Http\Middleware;

use App\Models\Company;
use Closure;
use NunoMazer\Samehouse\Facades\Landlord;

class SetTenant
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->check()) {
            if (auth()->user()->company_id) {
                Landlord::addTenant('company_id', \auth()->user()->company_id);
            }
        }

        return $next($request);
    }
}

Don´t forget to add the middleware to desired middleware stack in app/Http/Midleware/Kernel.http

Removing a tenant

To remove a tenant and stop scoping by it, simply call removeTenant():

Landlord::removeTenant('tenant_id');

// Or you can again pass a Model instance:
$tenant = Tenant::find(1);

Landlord::removeTenant($tenant);

You can also check whether Landlord currently is scoping by a given tenant:

// As you would expect by now, $tenant can be either a string column name or a Model instance
Landlord::hasTenant($tenant);

And if for some reason you need to, you can retrieve Landlord's tenants:

// $tenants is a Laravel Collection object, in the format 'tenant_id' => 1
$tenants = Landlord::getTenants();

Setting up your Models

To set up a model to be scoped automatically, simply use the BelongsToTenants trait:

use Illuminate\Database\Eloquent\Model;
use NunoMazer\Samehouse\BelongsToTenants;

class ExampleModel extends Model
{
    use BelongsToTenants;
}

If you’d like to override the tenants that apply to a particular model, you can set the $tenantColumns property:

use Illuminate\Database\Eloquent\Model;
use NunoMazer\Samehouse\BelongsToTenants;

class ExampleModel extends Model
{
    use BelongsToTenants;
    
    public $tenantColumns = ['tenant_id'];
}

Creating new Tenant scoped Models

When you create a new instance of a Model which uses BelongsToTenants, Landlord will automatically add any applicable Tenant ids, if they are not already set:

// 'tenant_id' will automatically be set by Landlord
$model = ExampleModel::create(['name' => 'whatever']);

Querying Tenant scoped Models

After you've added tenants, all queries against a Model which uses BelongsToTenant will be scoped automatically:

// This will only include Models belonging to the current tenant(s)
ExampleModel::all();

// This will fail with a ModelNotFoundForTenantException if it belongs to the wrong tenant
ExampleModel::find(2);

Note: When you are developing a multi tenanted application, it can be confusing sometimes why you keep getting ModelNotFound exceptions for rows that DO exist, because they belong to the wrong tenant.

Landlord will catch those exceptions, and re-throw them as ModelNotFoundForTenantException, to help you out :)

If you need to query across all tenants, you can use allTenants():

// Will include results from ALL tenants, just for this query
ExampleModel::allTenants()->get()

Under the hood, Landlord uses Laravel's anonymous global scopes. This means that if you are scoping by multiple tenants simultaneously, and you want to exclude one of the for a single query, you can do so:

// Will not scope by 'tenant_id', but will continue to scope by any other tenants that have been set
ExampleModel::withoutGlobalScope('tenant_id')->get();

Disabling Tenant

If you need to disable Tenant management, call disable method:

Landlord::disable();

To enable it again:

Landlord::enable();

Any time you need to check if tenant is enabled, use isEnabled method:

if (Landlord::isEnabled()) {
    // disable to execute admin tasks
    Landlord::disable();
    ...
    // enable again
    Landlord::enable();
}

Contributing

If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.

nunomazer/laravel-samehouse 适用场景与选型建议

nunomazer/laravel-samehouse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 256.1k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2021 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「tenant」 「multitenant」 「tenancy」 「multitenancy」 「landlord」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nunomazer/laravel-samehouse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nunomazer/laravel-samehouse 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 256.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 24
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 4
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-04