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

Garet's avatar
Level 3

Creating global variable from service provider

I'm creating a multi-domain application, so my application is served from multiple domains. Each domain has its own configuration like this:

app/config/domains.php

return [
    'www.domain1.com' => [
        'name' => 'UK',
        'default_language' => 'en',
        'languages_available' => ['en'],
        'email' => '[email protected]',
    ],
    'www.domain2.com' => [
        'name' => 'France',
        'default_language' => 'fr',
        'languages_available' => ['fr', 'en'],
        'email' => '[email protected]',
    ],
]

I want to be able to access a $domain variable from anywhere in my application that will give me the configuration for the current domain that is being used to access the application.

Here's how I've done it so far.

I've created a very simple Domain class which takes an array and converts it to an object as follows:

app/domain.php

class Domain {
    public function __construct($domain)
    {
        foreach ($domain as $key => $value)
            $this->$key = $domain;
        }
    }
}

Then I've created a DomainServiceProvider.php and in the boot method I do:

$host = request()->getHttpHost();

$this->domain = isset(config('domains')[$host]) ? config('domains')[$host] : [];

app->singletone('App/Domain', function($app) {
    return new Domain($this->domain);
});

Then in any of my controllers I can do:

class HomeController extends Controller
{
    public function show(Domain $domain)
    {

        if ($domain->name == 'UK') {
            // Do something here
        }

        // Return the view and pass $domain instance
        return view('show', [
            'domain' => $domain,
        ]);
    }
}
    

How does this seem? It works OK but I'm wondering if there is a more simple way? In particular is there a way og doing it without creating the Domain class which seems a little redundant.

0 likes
8 replies
bobbybouwmann's avatar
Level 88

In general, I would prefer to use a config file here instead. So let's say you have a file called config/acme.php. This file will contain some default values.

Then in your ServiceProvider you set the correct values in the config file.

Config::set('acme', $domainData); 

Then you can use the config helper or config class as dependency injection.

// helper
config('acme.name');

// dependency injection
use Illuminate\Config\Repository as Config;
public function (Config $config)
{
}

Your solution probably works fine as well. I think the config solution is a bit more flexible though ;)

2 likes
Garet's avatar
Level 3

@bobbybouwmann thanks, what I've ended up doing is ditching the Domains class and my service provider is as follows:

public function boot()
{

    $host = request()->getHttpHost();

    if (isset(config('domains')[$host])) {
        $this->domain = config('domains')[$host];
    }

    $this->app->singleton('domain', function($app) {
        return $this->domain;
    });
}

Then I can access the domain configuration everywhere, including in blade templates, like:

$domain = app('domain');
echo $domain['name'];

Or

app('domain')['name']

I guess this is very similar to what you're suggesting, but is it an abuse of the service container?

bobbybouwmann's avatar

Yeah, you're abusing the container now. It's fine I guess to do that. The benefit of using the config files is that you can already set a default and easily change them. You can even let them be set using your .env file.

Garet's avatar
Level 3

Thanks @bobbybouwmann I amended it to use config as you suggested because it seems like a better way.

However there is a snag. Now my error pages (for example 404 pages) don't have access to config whereas before they did have access to the service container. Do you know if there is a simple solution to that?

Garet's avatar
Level 3

Ignore my last comment, config is indeed available in the error exception views!

Please or to participate in this conversation.