Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Added default domain resolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
MSnoeren committed Jul 16, 2022
1 parent 0b2c3b0 commit 58929c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 17 additions & 2 deletions config/domain-scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types = 1);

use App\Models\Domain;
use Illuminate\Database\Eloquent\Model;

return [

Expand Down Expand Up @@ -79,6 +80,20 @@

'ignore' => array_map('strtolower', explode(',', env('DOMAINSCOPE_IGNORE', 'www'))),

/*
|--------------------------------------------------------------------------
| Default domain resolver.
|--------------------------------------------------------------------------
|
| If no (sub)domain could be matched, it's useful to fallback to a default
| domain. Configure your own resolver here.
|
*/

'default' => function (string $domain): ?Model {
return null;
},

/*
|--------------------------------------------------------------------------
| Scoped models.
Expand Down Expand Up @@ -111,8 +126,8 @@
| -----
| {domain} will be replaced by the lowercase variant of the domain, based
| on mode. It will insert the subdomain in sub mode and the full domain
| in full mode. You, as developer, can re-use this key to populate or clear
| the cache in your part of the application.
| in full mode. You can re-use this key to populate or clear the cache in
| your part of the application.
|
*/

Expand Down
5 changes: 5 additions & 0 deletions src/Http/Middleware/DetectDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function handle(Request $request, Closure $next) // phpcs:ignore
$resolver = app('domain-scope.resolver');
$model = $resolver->resolve($domain);

// If no domain has been matched, try the default fallback resolver.
if (!$model && is_callable($fallback = config('domain-scope.default'))) {
$model = $fallback($domain);
}

// If a domain has been matched, configure the application.
if ($model) {
// Bind the current domain into the service container.
Expand Down

0 comments on commit 58929c0

Please sign in to comment.