After Googling and reading many sites about Laravel multi-tenant, multisite, or whatever you want to call it, I decided I wanted a simple DIY method to add subfolders which could have its own database.
I did not want to mess around with apache, .htaccess…rather just add and modify a few lines of code here and there. With that said, here are some details regarding how we can reach our end goal. This tutorial reached those results using Laravel 5.7
With this minor changes below, we can easily have subdirectories use a private database. Thus, each subfolder is completely unique for user, passwords and content.
We can take this farther and write a simple script to automatically make these changes with a click of the mouse for any new subfolder we wish to create.
1. Add a second .env file with new name
2. Add a second routes file and name it like web2.php. These routes will be like the original file except the routes will include the subdirectory
3. Make changes to bootstrap/app.php. This code checks for the subfolder 'test' and returns the appropriate .env file.
Changes are shown below.
$app = new Illuminate\Foundation\Application(
$cur_dir = basename(dirname($_SERVER['REQUEST_URI']));
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']
=== 'on' ? "https" : "http") . "://" .
// Display the complete URL
$urlParts = explode('/', str_ireplace(array('http://', 'https://'), '', $url));
4. Make changes to app\Providers\RouteServiceProvider.php
Change the map method by adding the new method which will include the new routes file. This web route method call for $this->mapWebRoutes2(); must be above the original $this->mapWebRoutes();
Notice a the new method calling the web2.php route file too shown last.
public function map()
protected function mapWebRoutes2()