Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

booni3's avatar

Dynamic configuration in service provider

How would I best dynamically update the configuration used in a service provider? for example, if I have an API key for user1, API key for user2 running through the same service provider to connect to an external API service?

for example:

   public function register()
    {
        // Grab config
        $config = $this->app->config->get('xero.config');

        $this->app->bind('XeroPrivate', function () use ($config) {
            return new \XeroPHP\Application\PrivateApplication($config);
        });

    ...

I effectively need to be able to switch out the values inside xero.config for different users. Ideally I would use the same service provider and just update the config file prior to instantiating the service provider, but guess I could also store credentials in the database if needed.

0 likes
7 replies
OriOn's avatar

If every user has is own different value then yes the database would be my preference here

booni3's avatar

How would you suggest the best way to inject each user configuration from the database? Would I need to instantiate/extend the service provider before any logic is run?

Snapey's avatar

you dont have to get the api keys from config? Get them from the user model or related table.

$config = Auth::user->xero;

You should wrap this in a check that the user is authenticated

booni3's avatar

@snapey thanks for this.

So I can reference the user within the callback of the bind/singleton methods in the service provider. Thats great for getting each of the user configuration.

If I wanted to switch configuration independent of the user (i.e. if a user has 2 sets of credentials), is there any way to push new configs into the service provider? I realise service providers run first, before any of my logic, so what I am asking to do is update the service provider class configuration after it has already been instantiated and configured.

To give an example, on way I could do this is set a cache key with the required configuration and pull it back out in the service provider, but I expect this has flaws and is not best practice!

Service provider

        $this->app->bind(App\Library\MyClass::class, function () {
            return new App\Library\MyClass(\Cache::get('config'));
        });

In this particular instance, this class must be configured on instantiation, so to re-configure I effectively need to overwrite/update the class that was bound to the container, within my logic.

ravickalex's avatar

Hey, i have exactly the same issue. Did you find solution for this?

booni3's avatar

I am currently doing this in 2 ways. In both cases the configs are stored in a database.

  1. Within the service provider you use the user to lookup against the table and pull back the correct config. i.e. Auth::user->xero;

  2. You modify the flow of the script to push in the config before anything is run. I am doing this in most cases. i.e. in the constructor $xero = Xero::make($config)

1 like

Please or to participate in this conversation.