karlmonson/tenant
Composer 安装命令:
composer require karlmonson/tenant
包简介
Tenant allows for storing configuration settings in the database. This package is perfect for multitenant applications.
README 文档
README
Tenant allows for storing configuration settings in the database. This package is perfect for multitenant applications.
Installation
Install via Composer:
$ composer require "karlmonson/tenant": "~1.0"
Configuration
Register the Service Provider:
Karlmonson\Tenant\TenantServiceProvider::class,
Then register the Facade:
'Tenant' => Karlmonson\Tenant\Facades\Tenant::class,
Then run migrations to create the Tenant database table:
php artisan migrate
Usage
Seeding
To get started you may use the TenantTableSeeder provided by running the following command:
php artisan tenant:seed
This will seed the Tenant table with the default 3rd party config variables found the in .env file.
Creating/Updating Keys
To store a new key/value in the Tenant table, you may use the set method on the Tenant Facade:
<?php namespace App; use Karlmonson\Tenant\Facades\Tenant; class Store { public function setKey($key, $value, $encrypt = false, $env = false) { Tenant::set($key, $value, $encrypt, $env); } }
The set function will both store a new key/value pair as well as update an already existing pair.
If the encrypt flag is set to true, the value will be stored using the Laravel encrypt() method. When you retrieve the value from Tenant, it will automatically filter through the decrypt() method. It is recommended to always use the encrypt flag for Passwords, API Secrets, and any other sensitive information.
If the env flag is set to true, it will let Tenant know that it is a System/Package config option that should be used in place of setting the value in the .env file.
You may also use the Artisan command to set/update keys:
php artisan tenant:set key value --encrypt=0|1 --environment=0|1
Retrieving Keys
To retrieve a key/value pair in the Tenant table, you may use the get method on the Tenant Facade:
<?php namespace App; use Karlmonson\Tenant\Facades\Tenant; class Store { public function getKey($key, $default = null) { Tenant::get($key, $default); } }
The get method functions similarly to the Config::get() method by allowing you to pass a default value that will be returned if the value of the key is null or if the key does not exist.
You may also use the Artisan command to get keys, practical for testing/debugging purposes:
php artisan tenant:get key
Or you may also list all the keys in the Tenant table with the following command:
php artisan tenant:list
Using the Stored Config Values
This is probably the most important part of Tenant, and certainly the most magical.
When using Tenant for 3rd party services, you may use the values by calling the swapConfig method on the Tenant Facade:
<?php namespace App\Http\Controllers; use App\Mail\TestMail; use Illuminate\Support\Facades\Mail; use Karlmonson\Tenant\Facades\Tenant; class TestController extends Controller { public function sendMail() { Tenant::swapConfig(); Mail::to('test@example.com')->send(new TestMail()); } }
The swapConfig method will load the stored values into their respective configs for the current request. They are not permanently added to the configs making it still possible to use the values from the .env file if you so desire.
In order for the key/values stored in Tenant to be swapped successfully, the must follow the same structure as listed in the .env file. (i.e. MAIL_DRIVER or REDIS_HOST)
Contributing
Thank you for being willing to contribute to Tenant. You can read the contribution guidelines here.
Credits
- Karl Monson - Author
License
The MIT License (MIT). Please see License File for more information.
karlmonson/tenant 适用场景与选型建议
karlmonson/tenant 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 28, 最近一次更新时间为 2016 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「laravel」 「Settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 karlmonson/tenant 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 karlmonson/tenant 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 karlmonson/tenant 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Persistent settings package for Laravel framework.
Store your language lines in the database, yaml or other sources
A Zend Framework module to quickly and easily set PHP settings.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Settings Card for Laravel Nova.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-19