snoeren-development/laravel-domain-scope

This package is abandoned and no longer maintained. No replacement package was suggested.

Scope your platform's data by domain.

v1.4.0 2023-02-17 15:09 UTC

This package is auto-updated.

Last update: 2024-04-17 17:51:15 UTC


README

Latest version on Packagist Software License Build status Downloads

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

  1. Install the package using Composer:
composer require snoeren-development/laravel-domain-scope
  1. You should then publish all assets to your application using:
php artisan vendor:publish --provider="SnoerenDevelopment\DomainScope\ServiceProvider"
  1. Configure the settings in domain-scope.php to your likings.
  2. Update the migration if you need more information per domain. Reflect this in the model too.
  3. Configure the scoped models in domain-scope.php by adding their class names.
  4. Add the SnoerenDevelopment\DomainScope\Http\Middleware\DetectDomain to your (global) middleware stack.
  5. 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 8.0 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.