Skip to content

Instantly share code, notes, and snippets.

@HelgeSverre
Last active June 13, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HelgeSverre/0ca3c0b06673abfd5780705b935075b0 to your computer and use it in GitHub Desktop.
Save HelgeSverre/0ca3c0b06673abfd5780705b935075b0 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use App\Tenant;
use Closure;
use HipsterJazzbo\Landlord\Facades\Landlord;
class TenantHostnameScope
{
public function handle($request, Closure $next)
{
// Fetch the current subdomain
$subdomain = $this->getSubdomain($request);
// Check if any tenant has this hostname
$tenant = Tenant::where("hostname", $subdomain)->first();
if (!$tenant) {
abort(404);
}
// Scope all subsequent queries to that tenant
Landlord::addTenant($tenant);
return $next($request);
}
protected function getSubdomain($request)
{
$parts = explode(".", $request->getHost());
if (!count($parts) === 3) {
abort(404);
}
return array_first($parts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment