PHPackages                             gecche/laravel-multidomain - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Admin Panels](/categories/admin)
4. /
5. gecche/laravel-multidomain

ActiveLibrary[Admin Panels](/categories/admin)

gecche/laravel-multidomain
==========================

Laravel App on a subdomains, multi-tenancy setting

v13.0(3mo ago)1.1k251.9k—2.4%134[10 issues](https://github.com/gecche/laravel-multidomain/issues)1MITPHP

Since Jun 30Pushed 1mo ago32 watchersCompare

[ Source](https://github.com/gecche/laravel-multidomain)[ Packagist](https://packagist.org/packages/gecche/laravel-multidomain)[ RSS](/packages/gecche-laravel-multidomain/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (12)Versions (92)Used By (1)

[![License](https://camo.githubusercontent.com/30597ff9a350144f03bffdd9183e16468e0b3ca1193e1d08591d992622738d55/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://tldrlegal.com/license/mit-license)[![Laravel](https://camo.githubusercontent.com/40236e2476ff414887c1d4654db9142ebac4bce19aac8cd02eb1c6d2762deb09/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31332e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/3ccc671eca69e8695910518f858e08d1f046a75884b69d4821d3ccfcfee3db83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31322e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/7894171e4ea959fd933d11c81799acb9b535d48d90bf7bfbc40edefda04374f7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31312e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/dc5a02724cce542dca6f1dfd917d7e4dfde8b09e92de6e6a1afe61453eb1c4f4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31302e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/f56ee00d0d7664ddea5ef18ba730624857cb9f9a265075a09582d7beaeb8a6c8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d392e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/8ce0ee8a49efc56b84805856cacdfb6b6dce2a11f7ee9c0b2cf359239298685c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d382e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/88c14355c6e93c8bf2cb0ae3d78d3934b152943c57124aadbc0fe65e034fb212/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d372e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/d5aa7cb092b46d050964e450a59e858f65b3d9b1f501bdff0d634f2c79d549ab/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d362e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)[![Laravel](https://camo.githubusercontent.com/7d48a347cda33127449307a1ba24bdebf76d226c4f2a1585285c8e968f2666a9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d352e782d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](http://laravel.com)

Laravel Multi Domain
====================

[](#laravel-multi-domain)

An extension for using Laravel in a multi domain setting

[![Laravel Multi Domain](laravel-multidomain.png)](laravel-multidomain.png)

Description
-----------

[](#description)

This package allows a single Laravel installation to work with multiple HTTP domains.

There are many cases in which different customers use the same application in terms of code but not in terms of database, storage and configuration.

This package gives a very simple way to get a specific env file, a specific storage path and a specific database for each such customer.

Documentation
-------------

[](#documentation)

### Version Compatibility

[](#version-compatibility)

LaravelMultidomain13.x13.x12.x12.x11.x11.x10.x10.x9.x5.x8.x4.x7.x3.x6.x2.x5.8.x1.4.x5.7.x1.3.x5.6.x1.2.x5.5.x1.1.x#### Further notes on Compatibility

[](#further-notes-on-compatibility)

Releases v1.1.x:

- From v1.1.0 to v1.1.5, releases are fully compatibile with Laravel 5.5, 5.6, 5.7, 5.8 or 6.0.
- From v1.1.6+ releases v1.1.x are only compatible with Laravel 5.5 in order to run tests correctly.

To date, releases v1.1.6+, v1.2.x, v1.3.x, v1.4.x, v2.x and v3.x are functionally equivalent. Releases have been separated in order to run integration tests with the corresponding version of the Laravel framework.

However, with the release of Laravel 8, releases v1.1.14, v1.2.8, v1.3.8 and v1.4.8 are the last releases including new features for the corresponding Laravel 5.x versions (bugfix support is still active for that versions). **2021-02-13 UPDATE**: some last features for v1.1+ releases are still ongoing :)

v1.0 requires Laravel 5.1, 5.2, 5.3 and 5.4 (no longer maintained and not tested versus laravel 5.4, however the usage of the package is the same as for 1.1)

**2023-02-20 UPDATE**: From Laravel 10.x up, the package versions follow the same numbering.

### Installation

[](#installation)

Add gecche/laravel-multidomain as a requirement to composer.json:

```
{
    "require": {
        "gecche/laravel-multidomain": "13.*"
    }
}
```

Update your packages with composer update or install with composer install.

You can also add the package using `composer require gecche/laravel-multidomain` and later specify the version you want.

This package needs to override the detection of the HTTP domain in a minimal set of Laravel core functions at the very start of the bootstrap process in order to get the specific environment file. So this package needs a few more configuration steps than most Laravel packages.

Installation steps:

1. replace the whole Laravel container by modifying the following lines at the very top of the `bootstrap/app.php` file.

```
//use Illuminate\Foundation\Application
use Gecche\Multidomain\Foundation\Application
```

2. Override the `QueueServiceProvider` with the extended one in the `config/app.php` file as follows:

```
    'providers' => \Illuminate\Support\ServiceProvider::defaultProviders()->merge([
        // Package Service Providers...
    ])->replace([
      \Illuminate\Queue\QueueServiceProvider::class => \Gecche\Multidomain\Queue\QueueServiceProvider::class,
    ])->merge([
        // Added Service Providers (Do not remove this line)...
    ])->toArray(),
```

Please note that if you changed the `config/app.php` file due to other reasons, probably there is already the above `providers` entry in that file and the only important line is the one which replaces the `QueueServiceProvider`.

3. Publish the config file.

```
php artisan vendor:publish

```

(This package makes use of the discovery feature.)

Following the above steps, your application will be aware of the HTTP domain in which is running, both for HTTP and CLI requests, including queue support.

NOTE: in Laravel 11 the installation is simpler than before: if you use a previous version of Laravel, please check in the documentation the installation steps.

#### Laravel Horizon installation:

[](#laravel-horizon-installation)

The package is compatible with Horizon, thatnks to community contributions. If you need to use this package together with Horizon you have to follow other two installation steps:

1. Install Laravel Horizon as usual
2. Replace the Laravel Horizon import at the very top of the app/Providers/HorizonServiceProvider.php file.

```
//use Laravel\Horizon\HorizonApplicationServiceProvider;
use Gecche\Multidomain\Horizon\HorizonApplicationServiceProvider;
```

### Usage

[](#usage)

This package adds three commands to manage your application HTTP domains:

#### `domain.add` artisan command

[](#domainadd-artisan-command)

The main command is the `domain:add` command which takes as argument the name of the HTTP domain to add to the application. Let us suppose we have two domains, `site1.com`and `site2.com`, sharing the same code.

We simply do:

```
php artisan domain:add site1.com

```

and

```
php artisan domain:add site2.com

```

These commands create two new environment files, `.env.site1.com` and `.env.site2.com`, in which you can put the specific configuration for each site (e.g. databases configuration, cache configuration and other configurations, as usually found in an environment file).

The command also adds an entry in the `domains` key in `config/domains.php` file.

In addition, two new folders are created, `storage/site1_com/` and `storage/site2_com/`. They have the same folder structure as the main storage.

Customizations to this `storage` substructure must be matched by values in the `config/domain.php` file.

#### `domain.remove` artisan command

[](#domainremove-artisan-command)

The `domain:remove` command removes the specified HTTP domain from the application by deleting its environment file. E.g.:

```
php artisan domain:remove site2.com

```

Adding the `force` option will delete the domain storage folder.

The command also removes the appropriate entry from, the `domains` key in `config/domains.php` file.

#### `domain.update_env` artisan command

[](#domainupdate_env-artisan-command)

The `domain:update_env` command passes a json encoded array of data to update one or all of the environment files. These values will be added at the end of the appropriate .env.

Update a single domain environment file by adding the `domain` argument.

When the `domain` argument is absent, the command updates all the environment files, including the standard `.env` one.

The list of domains to be updated is maintained in the `domain.php` config file.

E.g.:

```
php artisan domain:update_env --domain_values='{"TOM_DRIVER":"TOMMY"}'

```

will add the line `TOM_DRIVER=TOMMY` to all the domain environment files.

#### `domain.list` artisan command

[](#domainlist-artisan-command)

The `domain:list` command lists the currently installed domains, with their .env file and storage path dir.

The list is maintained in the `domains` key of the `config/domain.php` config file.

This list is automatically updated at every `domain:add` and `domain:remove` commands run.

#### `config:cache` artisan command

[](#configcache-artisan-command)

The config:cache artisan command can be used with this package in the same way as any other artisan command.

Note that this command will generate a file config.php file for each domain under which the command has been executed. I.e. the command

```
php artisan config:cache --domain=site2.com

```

will generate the file

```
config-site2_com.php

```

#### Domain-specific configuration files

[](#domain-specific-configuration-files)

In addition to per-domain `.env` files, you can provide domain-specific configuration files under the `config/domains/` folder. The file name must use the sanitized domain name (dots replaced with underscores), e.g. for `site1.com` create:

```
config/domains/site1_com.php

```

Example file:

```
