php - 从 Stancl/Laravel Tenancy 的租户域内的中心域获取意外数据
问题描述
我正在使用 Stancl/TenancyforLaravel 租赁系统。
我为租户和中央域用户创建了一个名为 PaymentSettings 的模型和迁移。
对于中心用户 =app\Models\System\Admin\PaymentSettings.php
对于租户用户 =app\Models\Tenant\Admin\PaymentSettings.php
我添加了运行良好的 crud 操作。在服务提供商中设置启动时的条带 API 时,我面临着挑战。当我从数据库中获取设置数据时
文件: app\Providers\TenancyServiceProvider.php
namespace App\Providers;
use App\Models\System\Admin\SmtpSettings as AdminSmtpSettings;
use App\Models\Tenant\Admin\AdvanceSettings;
use App\Models\Tenant\Admin\GeneralSettings;
use App\Models\Tenant\Admin\PaymentSettings;
use App\Models\Tenant\Admin\SmtpSettings;
...
public function boot()
{
$this->bootEvents();
$this->mapRoutes();
$this->makeTenancyMiddlewareHighestPriority();
$this->viewResource();
}
....
protected function viewResource(){
View::composer('*', function ($view) {
$this->setConfigData();
if(Schema::hasTable('general_settings')){
$generalSettings = GeneralSettings::findOrFail(1);
$advanceSettings = AdvanceSettings::findOrFail(1);
$view->with([
'tenant_general' => $generalSettings,
'tenant_advance' => $advanceSettings,
]);
}
});
}
protected function setConfigData()
{
// When tenancy exists fetch the data from the tenants data;
$smtpTableExists = Schema::hasTable('smtp_settings');
$smtpSettings = null;
if(tenancy()->tenant){
if($smtpTableExists){ $smtpSettings = SmtpSettings::findOrFail(1); }
// Stripe Payment settings
$paymentTableExists = Schema::hasTable('payment_settings');
if($paymentTableExists){
$paymentSettings = PaymentSettings::findOrFail(1);
// return dd($paymentSettings->key);
if($paymentSettings && $paymentSettings->method === 'Stripe'){
Config::set('services.stripe.key', $paymentSettings->key);
Config::set('services.stripe.secret', $paymentSettings->secret);
Config::set('services.stripe.webhook_secret', $paymentSettings->webhook_secret);
Stripe::setApiKey($paymentSettings->key);
}
}
} // ends tenants config
else { // fetch data from the central domain
if($smtpTableExists){ $smtpSettings = AdminSmtpSettings::findOrFail(1); }
}
// set configs
if($smtpSettings){
Config::set('mail.mailers.smtp.host', $smtpSettings->host);
Config::set('mail.mailers.smtp.port', $smtpSettings->port);
Config::set('mail.mailers.smtp.encryption', $smtpSettings->encryption);
Config::set('mail.mailers.smtp.username', $smtpSettings->username);
Config::set('mail.mailers.smtp.password', $smtpSettings->password);
Config::set('mail.from.address', $smtpSettings->from_address);
Config::set('mail.from.name', $smtpSettings->from_name);
}
}
....
因为我已经设置了 API 密钥,所以我应该在控制器中获取它,但不是这样,null
当我调用时,我在其他控制器中获取值Stripe::getApiKey()
我想也许引导文件运行不正常,我试图在租户域内的控制器构造函数中手动写入
use App\Models\Tenant\Admin\PaymentSettings;
public function __construct()
{
$this->middleware('auth');
$paymentSettings = PaymentSettings::findOrFail(1);
Stripe::setApiKey($paymentSettings->key);
return dd(Stripe::getApiKey()); // showing the incorrect value from the central domain
}
但问题并没有在这里结束,我得到的值是来自中央域的值,而不是控制器内的租户域。但是当我在租户控制器的任何其他方法中编写类似的东西时,它给出了正确的值。
use App\Models\Tenant\Admin\PaymentSettings;
public function index()
{
// return dd(Stripe::getApiKey()); // showing the incorrect correct value from central domain
$paymentSettings = PaymentSettings::findOrFail(1);
Stripe::setApiKey($paymentSettings->key);
return dd(Stripe::getApiKey()); // showing the correct value
return view(someviewfilename);
}
预期行为
当我在租户的域上时,我想获取租户的数据库数据,而不是中央域数据库数据。
您的设置- Laravel 版本:^7.0
- stancl/租户版本:^3.2
- 存储驱动:DB
- laravel/收银员:^12.5
解决方案
我从创作者那里得到了答案。 https://github.com/stancl/tenancy/issues/558
想想 Laravel 请求生命周期是如何工作的。
在下一行,您希望中间件已经执行?当然不是,Laravel 先执行服务提供者和控制器构造函数,然后才知道使用什么中间件。
推荐阅读
- mongodb - 在 MongoDB 中合并同一集合中的两个文档
- python - 在浮点数学中将数字除以其除数时,如果结果不是整数浮点数
- html - 我遇到了与对齐、内联块、图片和字体相关的问题
- arrays - 如何获取获取的 json 数据以在颤振中列出?
- python-3.x - 如何修改visualization_utils.py以中文显示class_name
- mysql - 获取每行的第二秒最高值
- python - 需要澄清 Python 中字符串的不变性
- azure-devops - Microsoft 托管代理上的 Microsoft 同步框架?
- javascript - 如何清理 Contenteditable 内部文本:Angular 8
- parsing - 从字符串节点红色中提取值