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.
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,
]
];
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 useexample.test
as it's (routing) base. Thename
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. |
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.