Laravel:Pusher的动态配置

huangapple go评论71阅读模式
英文:

Laravel: dynamic configuration for Pusher

问题

我正在尝试使我的Laravel应用程序(SaaS)中Pusher的配置动态化。
基本上我想为不同的帐户存储不同的Pusher配置,并根据用户调用相应的配置。

我尝试在运行时更改配置,使用config()->set('services.pusher.xxx', 'yyyy'),但无论在框架的哪个级别都不起作用,甚至在自定义的ServiceProvider中也不行。

我找到了Laravel的BroadcastManager并尝试重写createPusherDriver(),以便我可以使用用户的配置创建PusherBroadcaster的自定义实例,但我不确定如何做或在哪里放置它!

最佳实践/标准方法是什么?

英文:

I am trying to make the configuration for Pusher in my Laravel app (SaaS) dynamic.
Basically I want to store different Pusher configs for different accounts. 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.

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!

What is the best-practice/standard way to do that?

答案1

得分: 2

我一直在我的一个项目中使用类似这样的设置来设置自定义邮件配置:

注意:由于服务提供程序在应用程序中加载的顺序可能会有所不同,因此您的情况可能会有所不同。

创建一个名为app\Providers\ConfigProvider.php的服务提供程序

public function boot()
{
    $this->app->booted(function () {
        $shouldSetCustomConfig = true;
        // 从数据库设置配置值。
        if($shouldSetCustomConfig) {
            config([
                'mail.host' => Config::get('mail.host'),
                'mail.port' => Config::get('mail.port'),
            ]);
        }
    });
}
  • $this->app->booted()是一个简单的回调,它在应用程序启动后调用。这可能不会始终正常工作,因为我曾经看到过各种使用这个回调来执行各种操作的包。在这种情况下,注册的顺序很重要。请注意,不一定需要使用这个回调。可以直接调用config(['key' => 'newval']),它可能会按预期工作。
  • 上面的服务提供程序应该在您为其设置配置的提供程序之前加载。在上面的示例中,这将是Illuminate\Mail\MailServiceProvider::class。这应该确保正确的配置已加载。
英文:

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

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 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.
  • 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. This should make sure the correct config is loaded.

huangapple
  • 本文由 发表于 2020年1月6日 20:40:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定