I would like to use the same Laravel App (infra) for all customers but each customer/subdomain to have its own DB (same schema)
Example:
siteA.laravel.app => customerA DB
siteB.laravel.app => customerB DB
siteC.laravel.app => customerC DB
I have tried creating one .env file per customer and map it to the customer subdomain.
So basically, it created a folder called /envs and then add the below files
.env.siteA.laravel.app
.env.siteB.laravel.app
.env.siteC.laravel.app
Then, I am adding below code to the boostrap/app.php
if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST']))
{
$domain = $_SERVER['HTTP_HOST'];
if (isset($domain) )
{
$dotenv = \Dotenv\Dotenv::createImmutable( base_path(), '/envs/.env.'.$domain );
try
{
$dotenv->load();
} catch ( \Dotenv\Exception\InvalidPathException $e ) {
// No custom .env file found for this domain
}
}
}
And the web.php would like like this...
Route::domain('{site}.laravel.app')->group(function ( $site ) {
//Routes go here
});
Finally, i added below to the .htaccess
<Files .env>
order allow,deny
Deny from all
</Files>
All works....But question I have is....
Have you done this ? if so, what approach have you used?
If you haven't done it...what issues do you see with above approach? Mainly...security.