You can effortlessly do this by using this package Gecce Multi Domain
Installation
Add gecche/laravel-multidomain as a requirement to composer.json:
{
"require": {
"gecche/laravel-multidomain": "4.*"
}
}
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 (for now, dev-v1.1.* is your best bet).
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:
replace the whole Laravel container by modifying the following lines at the very top of the bootstrap/app.php file.
//$app = new Illuminate\Foundation\Application(
$app = new Gecche\Multidomain\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
update the two application Kernels (HTTP and CLI).
At the very top of the app/Http/Kernel.php file , do the following change:
//use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Gecche\Multidomain\Foundation\Http\Kernel as HttpKernel;
Similarly in the app/Console/Kernel.php file:
//use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Gecche\Multidomain\Foundation\Console\Kernel as ConsoleKernel;
Override the QueueServiceProvider with the extended one in the $providers array in the config/app.php file:
//Illuminate\Queue\QueueServiceProvider::class,
Gecche\Multidomain\Queue\QueueServiceProvider::class,
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.
Usage
This package adds three commands to manage your application HTTP domains:
domain.add 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
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