Skip to content

Instantly share code, notes, and snippets.

@davidrushton
Created May 31, 2016 07:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davidrushton/b7c6748d90ad3bce044579ade30e3a98 to your computer and use it in GitHub Desktop.
Save davidrushton/b7c6748d90ad3bce044579ade30e3a98 to your computer and use it in GitHub Desktop.
Laravel multi-tenant multi-database
<?php
//config/database.php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'site',
'connections' => [
'base' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'secret'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'site' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => '',
'username' => env('DB_USERNAME', 'homestead'),
'username' => env('DB_USERNAME', 'secret'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
'migrations' => 'migrations',
];
<?php
//app/Http/Middleware/LoadSite.php
namespace App\Http\Middleware;
use App\Exceptions\SiteNotFoundException;
use App\Sites\SiteRepository;
use App\Sites\SiteService;
use Closure;
use Illuminate\Contracts\View\Factory as ViewFactory;
class LoadSite
{
/**
* @var SiteRepository
*/
private $site;
/**
* @var SiteService
*/
private $service;
/**
* @var View
*/
private $view;
/**
* Create a new filter instance.
*
* @param SiteRepository $site
* @param SiteService $service
* @param ViewFactory $view
*/
public function __construct(SiteRepository $site, SiteService $service, ViewFactory $view)
{
$this->site = $site;
$this->service = $service;
$this->view = $view;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$domain = $request->getHost();
if ( $domain !== env('APP_DOMAIN') ) {
$site = $this->site->findWithDomain($domain);
if ( ! $site) {
throw new SiteNotFoundException('Site with domain ' . $domain . ' not found');
}
$this->service->loadSite($site);
$this->view->share('site', $site);
}
return $next($request);
}
}
<?php
//app/Sites/Site.php
namespace App\Sites;
use Illuminate\Database\Eloquent\Model;
class Site extends Model {
protected $connection = 'base';
protected $table = 'sites';
// Removed methods for Gist
}
<?php
//app/Sites/SiteService.php
namespace App\Sites;
use Illuminate\Support\Facades\DB;
class SiteService {
// Removed other methods for Gist
public function loadSite(Site $site)
{
config()->set('session.connection', 'site');
config()->set('database.connections.site.database', $site->getDbName());
DB::connection('site')->setDatabaseName($site->getDbName());
if ( ! app()->environment('testing') ) {
DB::connection('site')->reconnect();
}
app('request')->site = $site;
config()->set('seo.title.default', $site->title);
config()->set('seo.title.suffix', $site->title);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment