-
-
Save kidker/e0b00324540aa6cf212ad00f3bceac74 to your computer and use it in GitHub Desktop.
Laravel multi-tenant multi-database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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