Single Laravel Code To Work With Multiple Domains


Your project works well when you have one user, one code, and one database. Right? Let's learn Single Laravel Code To Work With Multiple Domains.

As your product becomes very popular and more traffic comes to your product, how can you handle it for different users? 


Do you have any ideas? Do you want to ​​run the same project in different domains


I’ll share my thoughts, experience, and meaningful library. I’ve learned while developing the product in my company.

Here, @gecche is Laravel Multidomain package that is allows a single Laravel code to work with multiple HTTP domains.

There are many cases in which different customers are using 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:
  1. Specific .env file
  2. Specific storage path
  3. Specific database

Installation


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

{
    "require": {
        "gecche/laravel-multidomain": "2.*"
    }
}
 Update your packages with composer update or install with composer install.

2. 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__)
);

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

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

5. Publish the config file

php artisan vendor:publish

Usage

This package adds three commands to manage your application HTTP domains:
1. 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 
2domain.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

To get more information about Single Laravel Code To Work With Multiple Domains, please refer https://github.com/gecche/laravel-multidomain.

I hope this article will be useful for you and help you better understand how to create such kind of applications.

Thanks. Have a nice day!


Read Also:

Post a Comment

0 Comments