简体   繁体   English

Laravel:Pusher 的动态配置

[英]Laravel: dynamic configuration for Pusher

I am trying to make the configuration for Pusher in my Laravel app (SaaS) dynamic.我正在尝试在我的 Laravel 应用程序 (SaaS) 中动态配置 Pusher。 Basically I want to store different Pusher configs for different accounts.基本上我想为不同的帐户存储不同的 Pusher 配置。 And call the corresponding config based on the user.并根据用户调用相应的配置。

I have tries to change the config in runtime using config()->set('services.pusher.xxx', 'yyyy') , but this doesn't work at any level of the framework, event in a custom ServiceProvider.我尝试在运行时使用config()->set('services.pusher.xxx', 'yyyy')更改配置,但这在框架的任何级别都不起作用,在自定义 ServiceProvider 中的事件。

I found Laravel's BroadcastManager and tried to override the createPusherDriver() so that I could create a custom instance of PusherBroadcaster with the user's config, but I am not sure how to do that or where to put it!我找到了 Laravel 的BroadcastManager并尝试覆盖createPusherDriver()以便我可以使用用户的配置创建PusherBroadcaster的自定义实例,但我不知道该怎么做或把它放在哪里!

What is the best-practice/standard way to do that?这样做的最佳实践/标准方法是什么?

I've been using a setup like this in one of my own projects, to set a custom mail config:我一直在我自己的一个项目中使用这样的设置来设置自定义邮件配置:

NOTE: Your mileage may vary due to the order in which service providers are loaded in your app.注意:您的里程可能因服务提供商在您的应用程序中加载的顺序而异。

Create a serviceprovider like app\\Providers\\ConfigProvider.php创建一个像app\\Providers\\ConfigProvider.php这样的服务app\\Providers\\ConfigProvider.php

public function boot()
{
    $this->app->booted(function () {
        $shouldSetCustomConfig = true;
        // Set config values from database.
        if($shouldSetCustomConfig) {
            config([
                'mail.host' => Config::get('mail.host'),
                'mail.port' => Config::get('mail.port'),
            ]);
        }
    });
}
  • The $this->app->booted() is a simple callback that gets called after the application has been booted. $this->app->booted()是一个简单的回调,在应用程序启动后被调用。 This might not always work correctly because I've seen various packages that use this callback too to do various stuff.这可能并不总是正常工作,因为我已经看到各种包也使用这个回调来做各种事情。 When this is the case, the order of registration matters.在这种情况下,注册的顺序很重要。 Note that it is not required to use this callback.请注意,不需要使用此回调。 One might simply call the config(['key' => 'newval']) directly and it could work as intended.人们可能只是直接调用config(['key' => 'newval'])并且它可以按预期工作。
  • The service provider above should be loaded BEFORE the provider you are setting configuration for.上面的服务提供者应该在您为其设置配置的提供者之前加载。 In the example above it would be the Illuminate\\Mail\\MailServiceProvider::class .在上面的例子中,它是Illuminate\\Mail\\MailServiceProvider::class This should make sure the correct config is loaded.这应该确保加载了正确的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM