PHPackages                             democracyapps/domain-context - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. democracyapps/domain-context

ActivePackage[Utility &amp; Helpers](/categories/utility)

democracyapps/domain-context
============================

Utility to facilitate mapping parts of a route hierarchy to separate domains

1118PHP

Since Mar 25Pushed 11y ago2 watchersCompare

[ Source](https://github.com/DemocracyApps/domain-context)[ Packagist](https://packagist.org/packages/democracyapps/domain-context)[ RSS](/packages/democracyapps-domain-context/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

domain-context
==============

[](#domain-context)

This is a simple Laravel utility to facilitate mapping parts of a route hierarchy to separate domains.

As an example, let's say that your company has a site called coolmarketplace.com which lets other companies sign up to list their products. In addition to any pages you provide to highlight interesting or popular products, each company also has its own page, identified by a short-code that they pick. For example, Golly Gee Widgets has the page .

However, they also want that mini-site to show up under their own domain name, , probably with different styling, links, etc. You will need to have different application logic or presentation for the same mini-site depending on whether it's accessed as coolmarketplace.com/golly or market.gollygee.com.

The utility creates a singleton DomainContext object that can be used to test whether the current request is coming in via a domain that should be mapped differently from the default and to get the identifier for the internal object that is associated with that domain.

Instructions For Use
--------------------

[](#instructions-for-use)

### Installation

[](#installation)

Begin by installing this package through Composer.

```
{
    "require": {
        "democracyapps/domain-context": "dev-master"
    }
}
```

Add the service provider to app.php

```
// app/config/app.php

'providers' => [
    '...',
    'DemocracyApps\DomainContext\DomainContextServiceProvider',
];
```

For convenience, you may also add the Facade to app.php

```
'aliases' => [
    '...',
    'DomainContext' => 'DemocracyApps\DomainContext\DomainContextFacade',
    ];
```

### Configuration Parameters

[](#configuration-parameters)

Next, publish the configuration file by running

```
php artisan vendor:publish

```

and edit 'config/domain-context.php'. There are currently four configuration settings in use in the package.

#### mapped\_domain\_storage (default: 'config')

[](#mapped_domain_storage-default-config)

You may specify domains that are to be mapped in either the configuration file or in a database table. This parameter determines which is to be used. By default, mapped domains are simply added to the configuration file itself (see *mapped\_domains* parameter). If *mapped\_domain\_storage* is set to 'database', then it will look in the table specified by the *mapped\_domain\_table\_name* parameter.

#### mapped\_domains (default: empty array)

[](#mapped_domains-default-empty-array)

If *mapped\_domain\_storage* is set to 'config', then this array provides a list of the domains being mapped and the identifiers associated with them. The identifiers may be of any type. In the example below,  is a mapped domain and is associated internally with a short code of 'golly'. A more common use might be to associate it with an ID of a database model, as in the second line.

```
    'mapped_domains' => [
        'market.gollygee.com' => 'golly',
        'stdexample.com' => 133
    ]
```

#### mapped\_domain\_table\_name (default: 'mapped\_domains')

[](#mapped_domain_table_name-default-mapped_domains)

If *mapped\_domain\_storage* is set to 'database', information on mapped domains is searched in the table identified by this parameter. It expects the table to have a 'domain\_name' column and an 'identifier' column. It doesn't do anything with them - simply returns them when requested (see below) and interprets a match of the current domain with a table entry as indicating that the domain is mapped.

#### view\_variable\_name (default: 'domainContext')

[](#view_variable_name-default-domaincontext)

The DomainContext object is automatically made available to all Laravel views via, by default, the $domainContext variable. This parameter can be used to specify a different variable name.

### How To Use

[](#how-to-use)

Let's take the example at the top of this readme.

There are three common places where you'll actually implement variations in the logic or presentation in response to the incoming domain: the *routes.php* file, controllers, and views. The logic is your own, of course. You just need an easy way to tell whether you're in a mapped domain or not and, if so, what the associated client object should be.

The DomainContext object provides this via three methods: DomainContext::isMapped(), DomainContext::getDomain(), and DomainContext::getDomainIdentifier() (of course, you can also get the object via app()-&gt;make).

The first is a boolean that indicates whether the current domain is one that has been mapped. The second tells you what the domain is (in the example above, it would return 'market.gollygee.com'). The last returns the identifier to be used, e.g., to load client information from a table ('golly' in first example above).

To use them in the routes file, the simplest thing to do is use the facade. For example:

```
if (\DomainContext::isMapped()) {
    require app_path().'/Http/Routes/mapped.php'; // all the routes associated with mapped domains
}
else {
    require app_path().'/Http/Routes/market.php'; // all the routes associated with the platform
}
```

The facade can, of course, also be used in a controller or anywhere else, however, you can also simply inject the DomainContext object into your controller method, e.g.,

```
public function show($id, DomainContext $context)
{
    $short = $context->getDomainIdentifier();
    $tc = Company::where('short_name', '=', $short)->first();
    ...
```

Finally, within views, the DomainContext is always available as $domainContext (or the variable name you set in the configuration file). For example, you might have part of a menu depend on whether domain is mapped or not:

```

    @if ($domainContext->isMapped())
        All Products
    @else
        Market

            Products
            Companies

    @endif

```

Problems and Plans
------------------

[](#problems-and-plans)

This module is being used for a couple products in active development and will probably evolve. If you find bugs or have requests for features, create an issue here, find me on Twitter (@ejaxon) or submit a pull request.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ccf04ca37e5c3d51144fdaa975d9a702cfc84fba2e5df27906be69db56957e82?d=identicon)[ejaxon](/maintainers/ejaxon)

---

Top Contributors

[![ejaxon](https://avatars.githubusercontent.com/u/6409214?v=4)](https://github.com/ejaxon "ejaxon (18 commits)")

### Embed Badge

![Health badge](/badges/democracyapps-domain-context/health.svg)

```
[![Health](https://phpackages.com/badges/democracyapps-domain-context/health.svg)](https://phpackages.com/packages/democracyapps-domain-context)
```

###  Alternatives

[fylax/forceutf8

PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

22700.2k6](/packages/fylax-forceutf8)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
