placetopay / cerberus
允许在Laravel应用程序上有多个租户
Requires
- php: ^8.0
- ext-json: *
- eduarguz/shift-php-cs: ^3.0
- spatie/laravel-multitenancy: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- laravel/legacy-factories: ^1.0.4
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
- dev-main
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- v2.2.0
- 2.1.0
- 2.0.2
- v2.0.1
- v2.0.0
- 1.8.7
- v1.8.6
- v1.8.5
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5
- v1.4
- v1.3
- v1.2
- v1.1
- v1.0
- dev-bugfix/ignore-index-php-from-domain
- dev-feature/upgrade-php-cs-fixer
- dev-feature/cache-prefix
- dev-fix/mailer-loading
- dev-develop
- dev-feature/github-actions
- dev-feature/upgrade-spatie-multitenancy
- dev-feature/add-reset-tasks
- dev-feature/command-without-slashes
- dev-feature/merge-2.0.2
- dev-feature/adds-jobs-retry-request
- dev-bugfix/update-logger-context
- dev-feature/add-switch-mailer-task
- dev-bugfix/fix-storage-suffixed
- dev-feature/set-default-timezone-for-dates
- dev-feature/vapor-validation
- dev-feature/update-tasks
This package is auto-updated.
Last update: 2024-08-30 14:13:43 UTC
README
本包基于包spatie/laravel-multitenancy
的第三版。
因为它是定制化的,所以需要按照以下步骤进行覆盖才能正确安装。
本包旨在标准化房东数据库中tenants
表的配置,同时通过使用缓存来减少对同一数据库的查询次数。
先决条件
php8.0+
Laravel 8.0+
安装
本包可以通过composer安装
composer require "placetopay/cerberus:^3.0"
发布配置文件
您必须发布配置文件
php artisan vendor:publish --tag="multitenancy-config"
发布迁移文件
php artisan vendor:publish --tag="multitenancy-migrations"
按租户创建存储文件夹
只有当应用程序的配置变量multitenancy.suffix_storage_path
设置为true时,才允许运行此操作。
php artisan tenants:skeleton-storage --tenant=*
如何使用
发布配置和迁移文件后,您需要在config/database.php
中创建一个新的连接,该连接将允许管理房东数据库,其中将存储应用程序的租户。
'connections' => [
...
'landlord' => [
'driver' => env('DB_LANDLORD_DRIVER', 'mysql'),
'url' => env('DB_LANDLORD_URL'),
'host' => env('DB_LANDLORD_HOST', '127.0.0.1'),
'port' => env('DB_LANDLORD_PORT', '3306'),
'database' => env('DB_LANDLORD_DATABASE', 'forge'),
'username' => env('DB_LANDLORD_USERNAME', 'forge'),
'password' => env('DB_LANDLORD_PASSWORD', ''),
'unix_socket' => env('DB_LANDLORD_SOCKET', ''),
//...
],
...
]
修改了与spatie包相关联的房东表迁移,添加了一个json类型的config
字段,旨在将针对每个租户执行的前端配置集中化,在此字段中,您可以使用以下结构定义数据库连接。
{ "app": { "url": "...", "name": "..." }, "database": { "connections": { "mysql": { "host": "...", "port": "...", "database": "...", "username": "..." } } } }
您可以添加所有需要的配置,这个json将转换为数组点结构,然后将其设置在laravel配置中。
此外,在config/multitenancy.php
文件中提供了一个变量APP_IDENTIFIER
,它将是项目的标识符
执行迁移
要执行房东数据库的迁移,需要指定连接和迁移所在的文件夹路径
php artisan migrate --database=landlord --path=database/migrations/laandlord/
作业
您需要更新作业和failed_jobs的连接和表,在config/queue.php
[
//...
'connections' => [
'database' => [
'connection' => env('DB_LANDLORD_CONNECTION'),
'driver' => 'database',
'table' => '{project_identifier}_jobs',
'queue' => 'default',
'retry_after' => 90,
'after_commit' => false,
],
//...
]
//...
]
//...
'failed' => [
'driver' => env('QUEUE_FAILED_DRIVER', 'database'),
'database' => env('DB_LANDLORD_CONNECTION', 'landlord'),
'table' => '{project_identifier}_failed_jobs',
],
存储
默认情况下,本包将覆盖Storage Facade,将租户名称作为使用Storage Facade的文件夹的前缀,如果您需要将storage_path()
方法也添加后缀,您需要在config/multitenant.php
文件中将变量suffix_storage_path
设置为true。
如何更改命令
要执行一个特定租户的任何命令,需要执行以下命令结构php artisan tenants:artisan "command:execute" --tenant={tenant_domain}
添加--tenant={tenant_domain}
标志,将仅执行特定租户的命令,没有这个标志,将针对每个租户执行。
可翻译属性
您可以使用租户模型中的translate方法将配置JSON中的某些键翻译成其他语言。该方法使用应用程序的区域设置,并在JSON中找不到时回退以搜索正确的值,此外,您应该使用config/tenant.php
文件来设置翻译的默认值,以防JSON数据中不存在
数据库中的JSON数据
{ "tenant": { "terms_and_privacy": { "es_CO": "Al continuar acepto la <a class='underline' target='_blank' href='https: //www.placetopay.com/web/politicas-de-privacidad'> política de protección</a> de datos personales de <strong>Empresas del Grupo Evertec y sus filiales y subsidiarias</strong>" } } }
config/tenant.php
中的默认值
return [
'terms_and_privacy' => [
'en' => sprintf('By continuing, you accept the <a class="underline" target="_blank" href="%s"> personal data protection policy </a> of <strong>Companies of the Evertec Group and its affiliates and subsidiaries</strong>', 'https://www.placetopay.com/web/politicas-de-privacidad'),
'it' => sprintf('Continuando ad accettare la <a class="underline" target="_blank" href="%s"> politica di protezione dei dati personali </a> di <strong>Società del Gruppo Evertec e delle sue affiliate e sussidiarie</strong>', 'https://www.placetopay.com/web/politicas-de-privacidad'),
'pt' => sprintf('Ao continuar, você aceita a <a class="underline" target="_blank" href="%s"> política de proteção de dados pessoais </a> da <strong>Empresas do Grupo Evertec e suas afiliadas e subsidiárias</strong>', 'https://www.placetopay.com/web/politicas-de-privacidad'),
],
]
使用示例
app('currentTenant')->translate('terms_and_privacy')
远程清除缓存
可能您在更新租户信息时需要清除应用缓存,为此Cerberus发布了一个新的POST路由clean-cache
,您可以从其他应用程序调用此路由。
此路由使用中间件来验证应用程序是否可以连接,以下是一个您需要如何发起请求的示例
$data = [
'action' => 'cache:clear', //allowed action to perform
];
$signature = hash_hmac('sha256', json_encode($data), config('multitenancy.middleware_key'));
$url = 'https://tenant1.app.com/clean-cache';
Http::withHeaders(['Signature' => $signature])->post($url, $data);
您需要在请求中设置此头部以清除缓存