Last active
June 13, 2017 16:55
-
-
Save HelgeSverre/0ca3c0b06673abfd5780705b935075b0 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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