Scope your application's data by (sub-)domain.

Overview

Laravel Domain Scope

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 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.

You might also like...
Emoncms is an open-source web application for processing, logging and visualising energy, temperature and other environmental data and is part of the OpenEnergyMonitor project.
Emoncms is an open-source web application for processing, logging and visualising energy, temperature and other environmental data and is part of the OpenEnergyMonitor project.

Emoncms is an open-source web application for processing, logging and visualising energy, temperature and other environmental data and is part of the OpenEnergyMonitor project.

Add instagram feed to page from JSON Data
Add instagram feed to page from JSON Data

Custom Instagram Feed Add instagram feed to page from URL 🚧 Edit - As of 13th April 2021 - This code does not work. Solution is being looked into, ho

Division, District, Upazila/Thana and Union data of Bangladesh for Laravel application.

Bangladesh Geocode Division, District, Upazila/Thana and Union data of Bangladesh for Laravel application. Migration and seeders are ready. Just publi

A Zabbix 5.4 module to group items under Monitoring -> Latest data per Tag as it used to be with Application grouping in previous versions of Zabbix
A Zabbix 5.4 module to group items under Monitoring - Latest data per Tag as it used to be with Application grouping in previous versions of Zabbix

zabbix-module-latest-data Written according to Zabbix official documentation https://www.zabbix.com/documentation/current/manual/modules A Zabbix 5.4

Retrieve MySejahtera App's data from MySejahtera API and show to users via web browser. Written in PHP
Retrieve MySejahtera App's data from MySejahtera API and show to users via web browser. Written in PHP

MySejahtera-PHP-Web Retrieve MySejahtera App's data from MySejahtera API and show to users via web browser. Written in PHP. Disclaimer This web app is

The objective of this project is to manage The Website Manga, this site permits to Client to show, read and download Manga with the possibility to react, vote, and save his data.
The objective of this project is to manage The Website Manga, this site permits to Client to show, read and download Manga with the possibility to react, vote, and save his data.

The objective of this project is to manage The Website Manga, this site permits to Client to show, read and download Manga with the possibility to react, vote, and save his data.

An Online Movie Booking Website whose data is completely from a database

An Online Movie Booking Website whose data is completely from a database to ensure that it can be implemented in a real time scenario as any change of data needs to be done only in the database using SQL queries and the changes are immediately reflected.

Sign URLs with expiration date to transfer data between projects.

SafeLink Framework-agnostic, lightweight URL signer. You can use SafeLink to transfer data between projects. Sign the url in project 1: use Alfatron\S

Reporter - đź“Š Export Craft data as CSVs in a snap
Reporter - đź“Š Export Craft data as CSVs in a snap

Reporter 📊 Export Craft data as CSVs in a snap! ⚡️ Features 🏎 Reports are processed with Query Batching, making exports run quickly and without exha

Releases(v1.3.1)
Owner
Snoeren Development
Snoeren Development builds open source software for Laravel and Joomla!
Snoeren Development
HiDomainer One-File Domain Portfolio Script, is a PHP script of only 8kb.

HiDomainer One-File Domain Portfolio Script, is a PHP script of only 8kb. Download Feature 1. PC and Mobile Friendly 2. Data management without page r

HiDomainer.com 1 Mar 2, 2022
A framework for building rich, data-driven applications in PHP and MySQL

Xataface A framework for building rich, data-driven applications in PHP and MySQL License GPL Requirements PHP 5.2 or higher MySQL 5 or higher Install

Steve Hannah 129 Dec 13, 2022
Demo of using geo data in PHP web applications

phpgeo-demo Demo of using geo data in PHP web applications Goals Demonstrate basic data storage and retrieval methods using Geo specific data Orient t

J Corry 2 Jul 10, 2016
A much faster alternative to youtube-dl built for PHP applications.

youtube-downloader This project was inspired by a very popular youtube-dl python package: https://github.com/rg3/youtube-dl Yes, there are multiple ot

null 668 Jan 3, 2023
Exploit the vulnerability to install arbitrary applications in k61v1 without ROOT

k61v1injector Arbitrary application installer for Qin F21 Pro Exploit the vulnerability to install arbitrary applications in k61v1 without ROOT. Feel

Jim Wu 14 Nov 10, 2022
DBase - An easy-to-use backend for mobile and web applications

DBase DBase is an easy-to-use backend for your mobile and web applications, host the files in an Ubuntu server and use the SDKs to perform CRUD operat

Frank Eno ~ XSGames 39 May 25, 2022
Phalcon - DĂ©veloppez des applications web complexes et performantes en PHP

Phalcon - DĂ©veloppez des applications web complexes et performantes en PHP Ce projet GitHub contient les sources du livre : Phalcon 3 - DĂ©veloppez des

Les Enovateurs 3 Jul 5, 2022
CollectiveAccess is a web-based suite of applications providing a framework for management, description, and discovery of complex digital

README: Pawtucket2 version 1.7.14 About CollectiveAccess CollectiveAccess is a web-based suite of applications providing a framework for management, d

CollectiveAccess 70 Sep 28, 2022
QuidPHP/Core is a PHP library that provides an extendable platform to create dynamic applications

QuidPHP/Core About QuidPHP/Core is a PHP library that provides an extendable platform to create dynamic applications. It is part of the QuidPHP packag

QuidPHP 4 Jul 2, 2022