Skip to content

Commit efcd120

Browse files
committed
Add functionality to enable non-tenant subdomains to use tenant middleware when subdomain is enabled
1 parent b648be9 commit efcd120

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

config/laravel-multi-tenancy.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@
164164
*/
165165
'subdomain_key' => "domain",
166166

167+
/*
168+
*
169+
* The routes that use 'tenant' middleware but are under a
170+
* non-tenant subdomain.
171+
*
172+
* Example: 'cms.*', 'crm.*', 'blog.*'
173+
*
174+
* Tenant will be resolved from store rather than subdomain.
175+
*
176+
* Note: Make sure the user knows which tenant account they are
177+
* using.
178+
*
179+
*/
180+
'subdomain_safe_routes' => [
181+
// 'projects.*',
182+
],
183+
167184
/*
168185
*
169186
* The tenant routes prefix.

src/Http/Middleware/SetTenant.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public function handle($request, Closure $next, $guard = null)
2929

3030
list('in_tenant' => $inTenant, 'in_header' => $inHeader) = $options;
3131

32-
if (tenancy()->config()->getOption('routes.subdomain')) {
32+
$subdomainSafeRoutes = tenancy()->config()->getOption('routes.subdomain_safe_routes');
33+
34+
$subomainSafeCheck = empty($subdomainSafeRoutes) ? false : $request->routeIs($subdomainSafeRoutes);
35+
36+
if (tenancy()->config()->getOption('routes.subdomain') && empty($subomainSafeCheck)) {
3337
$value = ($request->{$subdomainKey});
3438
} else {
3539
$value = $inHeader ? $request->header(TenantStore::TENANT_HEADER) : tenancy()->store()->getKey($request->{$paramKey});

src/Tenancy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public function store()
7676
* @param $value
7777
* @return mixed
7878
*/
79-
public function resolveTenant($value)
79+
public function resolveTenant($value, $ignoreSubdomain = false)
8080
{
8181
$model = $this->tenantModel();
8282

83-
if (tenancy()->config()->getOption('routes.subdomain')) {
83+
if (tenancy()->config()->getOption('routes.subdomain') && $ignoreSubdomain == false) {
8484
$key = tenancy()->config()->getOption('routes.subdomain_key', 'domain');
8585
} else {
8686
$key = (tenancy()->config()->storeDriver() === 'db') ? config('tenancy.model.db_key') : config('tenancy.model.key');

0 commit comments

Comments
 (0)