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

foxws/laravel-multidomain

Repository files navigation

A Laravel extension for a multi domain/tenant setting

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Description

This package allows a single Laravel application to work with multiple domains/tenants.

It is intended to complement a multi-tenancy package such as spatie/laravel-multitenancy (tested), archtechx/tenancy, etc.

It allows caching of configs, routes & views, and is made to be easy to install, as there is no need to modify the core of the Laravel Framework.

TIP: See foxws/laravel-multidomain-demo for a boilerplate.

Installation

Install the package via composer:

composer require foxws/laravel-multidomain

Publish the config file with:

php artisan vendor:publish --tag="multidomain-config"

To prevent domain abuse, register the provided middlewares:

protected $middlewareGroups = [
    'web' => [
        // ...
        \Foxws\MultiDomain\Middlewares\NeedsDomain::class,
        \Foxws\MultiDomain\Middlewares\EnsureValidDomainSession::class,
    ]
];

Usage

The package will scan any subfolder (as in domain) located in app\Domain (customisable namespace) containing a domain.json file.

e.g. app\Domain\Example\domain.json:

{
    "name": "Example",
    "domain": {
        "local": "example.test",
        "staging": "example.dev",
        "production": "example.com"
    }
}

NOTE: The domain array matches the environment set in .env, e.g. APP_ENV=local will use example.test as it's (routing) base. The name is converted to a (slug) prefix and is to be used for registering components, routes, views, etc.

The structure of each domain should look like this, using app\Domain\Example as it's root path:

Path Description Cacheable
Routes\web.php The domain web routes.
Routes\api.php The domain api routes.
Config\*.php The domain config files.
Providers The domain providers (optional).
Resources\Components The domain Blade components (optional).
Resources\Translations The domain translation files (optional).
Resources\Views The domain Blade views (optional).

It will register each config, routes, views, components, using the domain's namespace in slug, e.g. example, foo-bar.

To interact with the domain(s), one may use the following:

Helper Description
config('example.app.name') Would return the name of the application.
route('example.home') Would return the route to /.
view('example::home') Would return the home.blade.php located in views.
<x-example::menu-component /> Would return the MenuComponent located in components.

When you have installed livewire/livewire, use the following helpers:

Helper Description
@livewire('example.component-name') @livewire blade directive.
<livewire:example.component-name /> <livewire: tag syntax.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.