Laravel Domain Scope
This package adds domain-scoped content to your application. Content will be available based on the current (sub)-domain allowing "multiple" websites to run off the same code base.
Installation
- Install the package using Composer:
composer require snoeren-development/laravel-domain-scope
- You should then publish all assets to your application using:
php artisan vendor:publish --provider="SnoerenDevelopment\DomainScope\ServiceProvider"
- Configure the settings in
domain-scope.php
to your likings. - Update the migration if you need more information per domain. Reflect this in the model too.
- Configure the scoped models in
domain-scope.php
by adding their class names. - Add the
SnoerenDevelopment\DomainScope\Http\Middleware\DetectDomain
to your (global) middleware stack. - Add (by default)
domain_id
to all your scoped models in the database. Use a constrained foreign id if you'd like to clear all data when removing a domain, for example:
$table->foreignId('domain_id')->constrained()->cascadeOnDelete();
Requirements
This package requires at least PHP 7.4 and Laravel 8.
Usage
After installation, it's up to you how to handle domains. For example:
- Create a middleware to throw a 404 when no domain has been matched.
- Letting a unmatched request through to your application's frontpage.
- Ignore
www
and no subdomain to show your frontpage. If another subdomain is available, throw a 404 when not found or present the application scoped by subdomain.
Service Container
If a domain has been matched and found, the service container receives two bindings: "domain" and the full configured model classname. You can use this to retrieve the currently active domain, for example:
$domain = app('domain');
Or in your controllers (or wherever the service container injects):
use App\Models\Domain;
public function index(?Domain $domain): Response
{
// $domain contains the matched domain or null if not matched.
}
Tip: Use middleware to enforce a matched domain into your routes to prevent null
being passed.
Models
Scoped models will only return results of the currently active domain. If no domain has been matched, all results will be returned as no scope has been applied.
Credits
License
The MIT license. See LICENSE for more information.