首页 > 解决方案 > 从 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);
   } 
预期行为

当我在租户的域上时,我想获取租户的数据库数据,而不是中央域数据库数据。

您的设置

标签: phplaravelstripe-paymentsmulti-tenant

解决方案


我从创作者那里得到了答案。 https://github.com/stancl/tenancy/issues/558

想想 Laravel 请求生命周期是如何工作的。

在这里,您定义了一些中间件细节:在此处输入图像描述

在下一行,您希望中​​间件已经执行?当然不是,Laravel 先执行服务提供者和控制器构造函数,然后才知道使用什么中间件。


推荐阅读