binary-cats / laravel-tenant
Requires
- php: ^7.2
- illuminate/support: ^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2021-10-11 02:37:50 UTC
README
此 Laravel 包允许您轻松实现多租户功能。它具有一定的意见性,您可以自由地更新和调整它。
功能:允许您创建一个以子域为驱动的多租户应用程序,其中每个租户都有一个专用的子域。您的模型可以是严格按租户划分的,或者共享给所有租户。您可以自由地关闭子域路由。
安装和设置
此包需要 PHP 7 和 Laravel 5.6 或更高版本。最新版本需要 PHP 7.2 和 Laravel 7 及以上。
php composer require binary-cats\laravel-tenant
包将自动注册自己。
资源
发布所有资源
php artisan vendor:publish --provider=BinaryCats\\LaravelTenant\\TenantServiceProvider
或者,分别发布
php artisan vendor:publish --provider=BinaryCats\\LaravelTenant\\TenantServiceProvider --tag=migrations php artisan vendor:publish --provider=BinaryCats\\LaravelTenant\\TenantServiceProvider --tag=config
配置
用法
要完全使用 Laravel Tenant,您需要准备您的模型和路由。Laravel Tenant 在认证环境中表现最佳;如果您计划在非认证环境中使用它,我建议创建一个 Service Provider,通过 TenantManager
解决租户问题,以防止任何数据泄露。
模型设置
任何期望全局作用域的模型需要
- 有一个外键租户键
- 导入可租户化特性
<?php namespace App\Models; use BinaryCats\LaravelTenant\Tenantable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Tenantable;
这将为模型添加一个(全局作用域)[https://laravel.net.cn/docs/7.x/eloquent#global-scopes] 和一个自动模型观察者,使用当前租户值填充模型。
或者,您可以使用 OptionalTenantable
特性;它将为租户键的精确匹配或模型租户键的空值进行范围限定。使用场景是当您有一个共享的模型集,而租户可能添加自己的模型。例如 Gmail 文件夹:收件箱和垃圾桶是每个用户邮箱中的标签,但您可以自由地添加更多标签,它们只能由您自己看到。
注意
不要将 tenant_id 包含在可填充属性中;它应该是受保护的,并且最好隐藏起来,以确保您不会意外地泄露租户信息。
事件设置
Laravel Tenant 提供了自动监听器,您可以将它们连接到应用程序中,以简化您的应用。
/* |-------------------------------------------------------------------------- | Binary Cats | Tenants table |-------------------------------------------------------------------------- | | Name of the table to hold the tenants */ 'listeners' => [ \Illuminate\Auth\Events\Authenticated::class => [ # Will register a tenant within TenantManager based on the tenant of the authenticated user \BinaryCats\LaravelTenant\Listeners\SetTenant::class, ], \Illuminate\Auth\Events\Logout::class => [ # Will de-register a tenant within TenantManager based on the tenant of the user being loged out \BinaryCats\LaravelTenant\Listeners\RemoveTenant::class, ], ],
子域
Laravel Tenant 提供了开箱即用的子域解析逻辑。如果不需要此功能,请将配置 tenant.routing.autobind
开关切换到 false
。
/* |-------------------------------------------------------------------------- | Binary Cats | Routing rules |-------------------------------------------------------------------------- | | Whenever a route is requested, one of the items needed is a subdomain. | */ 'routing' => [ # Set `autobind` to true to ensure it is resolved automatically 'autobind' => true, # Set `subdomainKey` to the name of the parameter to bind 'subdomainKey' => 'tenant', # If you need to replace the URL generator with yet another # Must be implemeting Illuminate\Contracts\Routing\UrlGenerator 'generator' => \BinaryCats\LaravelTenant\Routing\UrlGenerator::class, ],
测试
运行测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件info@binarycats.io联系,而不是使用问题跟踪器。
鸣谢
支持我们
Binary Cats是一家位于伊利诺伊州罗斯埃尔的公司。
您的业务依赖于我们的贡献吗?联系我们!所有承诺都将专门用于分配人力进行维护和新奇的功能。
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。