app开发者平台在数字化时代的重要性与发展趋势解析
1324
2022-11-01
Tenant:将Laravel配置存在数据库中,这个包很适合多租户应用程序
Tenant
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:
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:
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:
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.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~